faqts : Computers : Databases : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

40 of 62 people (65%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I use a shell script to make sure Mysql server is alive and restart it if it is not running?

Feb 12th, 2008 04:20
dman, josh oshiro, Brad Lay, Narendra Jain, Chris Ditty, http://sturly.com


You have to check if the mysql process is running with this command:

#ps -ef | grep mysql | grep -v grep
or
#ps -auxwww | grep mysql | grep -v grep

depending on which nix* your using.

Then check if the result of this command was 0 (success) or other than 
0 (failed).

If the result is other than 0 then it means that mysql is not running.

You should then start mysql with a command that looks something like 
this:

#mysql -u <user> --password='<password>'

This will do it all in one shot

#ps -ef | grep mysql | grep -v grep || mysql -u <user> --
password='<password>'

this says if mysql is not running run mysql

remember, if ps -ef does not work then try using ps -auxwww instead.



[added by brad]

I used this on my Debian 3.0 Unstable box, in cron.

0-59/1 * * * * ps -ef | grep mysqld | grep -v grep >> /dev/null 
|| /etc/init.d/mysql start

if its running, it just continues on its merry way, if its not, it 
starts it and emails you about it.

Works great. Nice and simple.