1

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.

1

There is a service named monit which do that as easy as it can be. You can configure it for any services for restarting that service for some conditions and it can alert you if its conditions dose not happen. Monit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.