Problems restarting MySQL when starting through crontab scheduler

I wrote a shell script that starts MySQL when it is killed / terminated. I am running this shell script using crontab.

My cron is looking for a 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 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 script shell manually using webmin crontab, MySQL started successfully (when it was killed).

However , when I plan to use it with the cron job, MySQL does not start. Logs print correctly (this means that my cron runs the scheduled script successfully, but MySQL does not restart).

crontab -l displays:

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

URL-, MySQL , cron. , .

- !

.

+4
1

-, crontab normaly :

* * * * * /root/mysql.sh 

, sh script - #!/bin/bash. ( sh bash?) (chmod +x /root/mysql.sh)

-, crontab , ! . PATH: echo $PATH, export PATH=<your path> 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

, , cron.

, , ( PATH) . set | less , cron script. - MYSQL , !. cron script, set > cron.env cron script, .

+1

All Articles