0

I have a serverpilot.io server running from digitalocean (1gb Ram), MySQL will crash once a week, the stats say the server is using ~256 mb out of 1 gb, but mysql will crash saying out of memory....

To solve this I found this script:

service mysql status| grep 'mysql start/running' > /dev/null 2>&1

#!/bin/bash
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
  /usr/sbin/service mysql start
fi

It works when I run it direct bash /srv/mysql.sh.

I have also run chmod +x /srv/mysql.sh

I have also tried:

* * * * * sh /srv/mysql.sh

* * * * * bash /srv/mysql.sh

* * * * * /usr/bin/sh /srv/mysql.sh

* * * * * /srv/mysql.sh

I am getting this email anytime it runs and mysql is not starting as it should

/srv/mysql.sh: 4: /srv/mysql.sh: [[: not found

when I run 'service mysql stop' and check with 'service mysql status' the mysql service remains stopped and non functional.

Any advice would be greatly appreciated

Edit 1: I added >>/srv/cronlog.log 2>&1 to the end of the crontab line

and I am getting this:

/srv/mysql.sh: 5: /srv/mysql.sh: [[: not found
/srv/mysql.sh: 5: /srv/mysql.sh: [[: not found
/srv/mysql.sh: 5: /srv/mysql.sh: [[: not found
/srv/mysql.sh: 5: /srv/mysql.sh: [[: not found

Edit 2: I changed to * * * * * bash /srv/mysql.sh >>/srv/cronlog.log 2>&1

 * Starting MySQL database server mysqld
   ...done.
 * Checking for tables which need an upgrade, are corrupt or were 
not closed cleanly.

Mysql is still stopped in testing

Edit 3: Tail of crontablog.log

 * Starting MySQL database server mysqld
   ...done.
 * Starting MySQL database server mysqld
   ...done.
 * Starting MySQL database server mysqld
   ...done.
 * Starting MySQL database server mysqld
   ...done.
7
  • Which of those lines are you using right now?
    – muru
    Jul 2, 2015 at 17:32
  • I have updated the question: Edit 2: I changed to * * * * * bash /srv/mysql.sh >>/srv/cronlog.log 2>&1 * Starting MySQL database server mysqld ...done. * Checking for tables which need an upgrade, are corrupt or were not closed cleanly. Mysql is still stopped in testing
    – exilepc
    Jul 2, 2015 at 17:51
  • I only have one instance of mysql running
    – exilepc
    Jul 2, 2015 at 17:55
  • How do you edit your cronjob or which file do you edit?
    – Cyrus
    Jul 2, 2015 at 18:04
  • crontab -e (it opens nano)
    – exilepc
    Jul 2, 2015 at 18:05

1 Answer 1

0

In searching around I came across a better script and decided to try it, and it works:

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
    echo "MySQL restarted" | mail -s "Email Subject" [email protected]
    sudo service mysql start
fi

More info can be found here http://cssjockey.com/how-to-create-a-cron-job-to-start-mysql-if-it-stops/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .