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)"が false かどうかを探していることを意味しますが、これは達成したいことではありません。サービス ステータス コマンドの結果が「start/waiting」を含む文字列ではないことを確認したいのです。は、!=等しくないことを示す最良の方法です。

また、 は=~正規表現を期待しており、表示されると予想される文字列の一部のみを指定します。 if ステートメントの二重括弧構文はシェルのグロブをサポートしているため、 を使用して「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* ]]

関連情報