Question

I have written a shell script which starts MySQL when its killed/terminated. I am running this shell script using a crontab.

My cron looks for the script file named mysql.sh under /root/mysql.sh

sh /root/mysql.sh  

mysql.sh:

cd /root/validate-mysql-status
sh /root/validate-mysql-status/validate-mysql-status.sh

validate-mysql-status.sh:

# mysql root/admin username
MUSER="xxxx"
# mysql admin/root password
MPASS="xxxxxx"
# mysql server hostname
MHOST="localhost"
MSTART="/etc/init.d/mysql start"
# path mysqladmin
MADMIN="$(which mysqladmin)"

# see if MySQL server is alive or not
# 2&1 could be better but i would like to keep it simple
$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
    # MySQL's status log file
    MYSQL_STATUS_LOG=/root/validate-mysql-status/mysql-status.log

    # If log file not exist, create a new file
    if [ ! -f $MYSQL_STATUS_LOG ]; then
        cat "Creating MySQL status log file.." > $MYSQL_STATUS_LOG
        now="$(date)"
        echo [$now] error     : MySQL not running >> $MYSQL_STATUS_LOG
    else
        now="$(date)"
        echo [$now] error     : MySQL not running >> $MYSQL_STATUS_LOG
    fi

    # Restarting MySQL
    /etc/init.d/mysql start

    now1="$(date)"
    echo [$now1] info     : MySQL started >> $MYSQL_STATUS_LOG
    cat $MYSQL_STATUS_LOG
fi

When I run the above mysql shell script manually using webmin's crontab, MySQL started successfully (when its killed).

However, when I schedule it using a cron job, MySQL doesn't starts. The logs are printed properly (it means my cron runs the scheduled script successfully, however MySQL is not restarting).

crontab -l displays:

* * * * * sh /root/mysql.sh 

I found from URL's that we should give absolute path to restart MySQL through schedulers like cron. However, it haven't worked for me.

Can anyone please help me!

Thank You.

Was it helpful?

Solution

First, crontab normaly looks like this:

* * * * * /root/mysql.sh 

So remove the surplus sh and put it at the beginning of the script - #!/bin/bash I suppose (why are you referring to sh instead of bash?) and don't forget to have an execute permission on the file (chmod +x /root/mysql.sh)

Second, running scripts within crontab is tricky, because the environment is different! You have to set it manually. We start with PATH: go to console and do echo $PATH, and then copy-paste the result into export PATH=<your path> to your cron script:

mysql.sh:

#!/bin/bash

export PATH=.:/bin:/usr/local/bin:/usr/bin:/opt/bin:/usr/games:./:/sbin:/usr/sbin:/usr/local/sbin
{
cd /root/validate-mysql-status
/root/validate-mysql-status/validate-mysql-status.sh
} >> OUT 2>> ERR

Note that I also redirected all the output to files so that you don't receive emails from cron.

Problem is how to know which other variables (besides PATH) matter. Try to go through set | less and try to figure out which variables might be important to set in the cron script too. If there are any MYSQL related variables, you must set them! You may also examine the cron script environment by putting set > cron.env to the cron script and then diff-ing it against console environment to look for significant differences.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top