mysql cron 作業 - 僅在重新啟動時發送電子郵件

mysql cron 作業 - 僅在重新啟動時發送電子郵件

我有以下 cron 作業,它每 5 分鐘運行一次(它實際上有效)

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ "$(/usr/sbin/service mysql status)" != *start/running* ]]
then
    echo "MySQL restarted" | mail -a FROM:*@*  -s "[Marketing Server] Database Service" *@*
    sudo service mysql start
fi

運行mysql時狀態(正在運行)

mysql.service - LSB: Start and stop the mysql database server daemon Loaded: loaded (/etc/init.d/mysql) Active: active (exited) since Wed 2015-12-09 15:01:40 GMT; 21h ago Docs: man:systemd-sysv-generator(8) Process: 30829 ExecStop=/etc/init.d/mysql stop (code=exited, status=0/SUCCESS) Process: 30910 ExecStart=/etc/init.d/mysql start (code=exited,status=0/SUCCESS)

Dec 10 12:10:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:15:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:20:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:25:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:30:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:35:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:40:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:45:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:50:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:55:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Hint: Some lines were ellipsized, use -l to show in full.

運行mysql時狀態(已停止)

david@MarketingServer:~$ sudo service mysql stop ^[[Adavid@MarketingServer:~$ sudo service mysql status ● mysql.service - LSB: Start and stop the mysql database server daemon Loaded: loaded (/etc/init.d/mysql) Active: inactive (dead) since Thu 2015-12-10 13:03:26 GMT; 1s ago Docs: man:systemd-sysv-generator(8) Process: 5024 ExecStop=/etc/init.d/mysql stop (code=exited, status=0/SUCCESS) Process: 30910 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS)

Dec 10 12:35:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:40:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:45:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:50:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:55:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 13:00:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 13:03:24 MarketingServer systemd[1]: Stopping LSB: Start and stop the.... Dec 10 13:03:24 MarketingServer mysql[5024]: * Stopping MySQL database serve...d Dec 10 13:03:26 MarketingServer mysql[5024]: ...done. Dec 10 13:03:26 MarketingServer systemd[1]: Stopped LSB: Start and stop the .... Hint: Some lines were ellipsized, use -l to show in full.

我遇到的唯一問題是它每次都會發送一封電子郵件,說明服務已重新啟動,即使沒有重新啟動。我希望它僅在服務實際從停止狀態重新啟動時發送。

誰能解釋一下我哪部分錯了

答案1

您的 if 語句中有幾個問題。條件測試的開頭!意味著您正在尋找表達式是否"$(/usr/sbin/service mysql status)"為假,這不是您想要實現的目標。您想要檢查服務狀態命令的結果是否不是包含「開始/等待」的字串。!=是表示不等於的最好方式。

另外,=~需要一個正規表示式,您只需提供您希望看到的字串的一部分。由於 if 語句的雙括號語法支援 shell 通配符,因此您可以使用 . 搜尋「start/waiting」*start/waiting*

如果你改變線路:

if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]

到:

if [[ "$(/usr/sbin/service mysql status)" != *start/running* ]]

您的腳本應該正常工作,否則,我認為您每次運行腳本時都會重新啟動服務並發送電子郵件。

編輯:

以下不是理想的解決方案,我發布它只是為了嘗試幫助您解決當前面臨的情況。

若要嘗試處理輸出,systemd-sysv-generator您可以嘗試將語句的第一行替換if為:

if [[ "$(/usr/sbin/service mysql status)" = *inactive* ]]

相關內容