Linux

無法從 cron 作業重新啟動 mysql

  • May 11, 2016

我有一個問題,MySQL(在 Ubuntu 12.04 上執行,所有最新更新)幾天后當機,所以將此腳本放入每 60 秒以 root 身份執行的 cron 作業中。

一切正常,除了 mysql 服務不會重新啟動,我嘗試使用“service mysql restart”和“/usr/bin/mysqld”,但它們也不起作用。

我正在使用重新啟動命令的完整路徑,所以我認為這不是問題。現在唯一可行的方法是用“重啟”替換重啟命令,這非常難看,我想避免它。

#!/bin/bash
#Check if MySQL is up, if not then start it
# mysql root/admin username
MUSER="root"
# mysql admin/root password
MPASS="REMOVED"
# mysql server hostname
MHOST="localhost"
#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
MSTART="/etc/init.d/mysql restart"
# Email ID to send notification
EMAILID="some@email.net"
# path mysqladmin
MADMIN="$(which mysqladmin)"

MAILMESSAGE="/tmp/mysql.fail.$$"

# see if MySQL server is alive or not

$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
       echo "" >$MAILMESSAGE
       echo "ALERT: MySQL Server is not responding to ping">>$MAILMESSAGE
       echo "Hostname: $(hostname)" >>$MAILMESSAGE
       echo "System Time: $(date)" >>$MAILMESSAGE
       # try to start mysql
       $MSTART>/dev/null
       # see if it is started or not
       o=$(ps cax | grep -c ' mysqld$')
       if [ $o -eq 1 ]; then
               sMess="MySQL server successfully restarted"
       else
               sMess="MySQL server FAILED to restart"
       fi
       # Email status too
       echo "Current Status: $sMess" >>$MAILMESSAGE
       echo "" >>$MAILMESSAGE
       echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
       echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
       # send email
       sendemail -o message-content-type=text -f some@email.net -t $EMAILID -u MySQL GURU ALARM -m < $MAILMESSAGE
else # MySQL is running :) and do nothing
       :
fi
# remove file
rm -f $MAILMESSAGE

如果沒有看到啟動錯誤日誌,幾乎無法診斷。

如果我不得不冒險猜測,這聽起來很可能有問題ulimit,該程序可能無法從 cron 分配它需要的記憶體,但在通過互動式 shell 執行時不受限制。MySQL 日誌將確認這一點,儘管是以非描述方式。

此外,不要使用半豎起的腳本重新發明輪子,只需使用公認的監控應用程序,如monit

引用自:https://serverfault.com/questions/401853