我有以下 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运行时状态(running)
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)"
为假,而这并不是您想要实现的。您想检查服务状态命令的结果是否不是包含“start/waiting”的字符串。!=
是不等于的最佳表示方式。
此外, 期望=~
使用正则表达式,您只需提供期望看到的字符串的一部分。由于 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* ]]