My system is centos 6.5,
I write a simple bash shell to check if mysql is crashed, restart service again. I put it in /home/myspace/mysql.sh
chown root:root /home/myspace/mysql.sh
then run every minute by crontab.
#!/bin/bash
mysql --host="localhost" --user="root" --password="password" --database="test" --execute="select id from test limit 1"
if [ $? -eq 0 ]; then echo "";
else
/usr/bin/sudo service mysql stop
pkill mysql
/usr/bin/sudo service mysql start
echo "error $(date)" >> /home/myspace/restart_log.txt
fi
Now I have 2 questions.
Why in my code,
if ... else ...
not working? I mean the mysql server has no problem, it can execute"select id from test limit 1"
and get result, but the script still run the code inelse
case.in
/var/log/sucure
, it showroot : sorry, you must have a tty to run sudo ; TTY=unknown ; PWD=/root ; USER=root ; COMMAND=/sbin/service mysql stop
.root : sorry, you must have a tty to run sudo ; TTY=unknown ; PWD=/root ; USER=root ; COMMAND=/sbin/service mysql start
. And the mysql server is down.
UPDATE: Now I tried the code @Anthon given:
#!/bin/bash
RESULT=`mysql --host="localhost" --user="root" --password="password" --database="test" --execute="select browser from test limit 1"`
if [ $? -eq 0 ]; then
echo "mysql select ok"
else
/usr/bin/echo "mysql select failed"
sudo service mysql stop
pkill mysql
/usr/bin/sudo service mysql start
echo "error $(date)" >> /home/myspace/restart_log.txt
fi
it works well in SSH
part (return mysql select ok)
but not working in cronjob
(every minute write error in /home/myspace/restart_log.txt
and mysql server never start)
Is this problem caused by PATH
? OR privileges
? How to solve it? thanks.
solve Thanks for all, finally I solved it. @Sigi's method maybe good, but not working in my situation. @Anthon's answer write better than I, but still not working. @Emmanuel, given the answer is more nearer than other's. After a long time test, I shared final working code to all.
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/local/bin
# cronjob need clear where is the path
# mysql under path /usr/local/bin
mysql --host="localhost" --user="root" --password="password" --database="test" --execute="select id from test limit 1"
if [ $? -eq 0 ]; then echo "";
# mysql run well nothing to do, make a
else
sudo /etc/init.d/mysql stop
pkill /usr/local/bin/mysql
# It should be pkill /usr/local/bin/mysql, not pkill mysql, wrong write will cause below code not working
sudo /etc/init.d/mysql start
sudo /etc/init.d/httpd restart
# Some strange behave, after mysql restart, apache will die, so should add httpd restart
echo "error $(date)" >> /home/myspace/restart_log.txt
fi
if...else
is obviously working. Your question is: Why does themysql
command not exit with code 0? You should make the second question a separate question as they are not related.echo "error $(date)" >> /home/myspace/restart_log.txt
. but it works directly inSSH
part. thanks.RESULT=`mysql --host="localhost" --user="root" --password="password" --database="test" --execute="select browser from test limit 1" 1>/tmp/mysql.debug 2>&1`
and see the output in /tmp/mysql.debug