【发布时间】:2018-01-05 10:01:01
【问题描述】:
这是我的第一篇文章,所以请耐心等待!
我尝试创建检查服务是否无法访问的脚本(http 错误代码),然后 Monit 应该重新启动程序(预览服务)。 Monit 以用户“spark”的身份运行。
这是 phantomjs-check.sh 代码:
#!/bin/bash
# source: /opt/monit/bin/phantomjs-check.sh
url="localhost:9001/preview/phantomjs"
response=$(curl -sL -w "%{http_code}\\n" $url | grep 200)
if [ "$response" = "}200" ]
then
echo "-= Toimii!!!! =-"
exit 1
else
echo "-= RiKKi!!!! =-"
exit 0
fi
[root@s-preview-1 bin]#
如果我手动终止 previewservice 并运行该脚本,我会得到 0 的退出代码,这是应该如何工作的。
在Monit中我有以下配置:
check program phantomjs with path "/opt/monit/bin/phantomjs-check.sh"
if status = 0 then exec "/opt/monit/bin/testi.sh"
目前我添加了一些日志记录,这是 test.sh 代码:
#!/bin/sh
# source: /opt/monit/bin/testi.sh
############# Added this for loggin purposes ############
#########################################################
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo Testi.sh run at $dt >> /tmp/testi.txt
# Original part of the script
sudo bash /opt/previewservice/preview-service.sh start
在 /etc/sudoers 文件中我有一行:
spark ALL=(ALL) NOPASSWD: /opt/previewservice/preview-service.sh
此命令在 cli 中运行,它启动/重新启动 previewservice。我可以手动运行“testi.sh”脚本作为 spark [spark@s-preview-1 bin]$ ./testi.sh 并且它按预期工作,但即使是 Monit 也得到服务已关闭的信息,它不会启动。
$ cat /tmp/testi.txt
Testi.sh run at 05/01/2018 10:30:04
Testi.sh run at 05/01/2018 10:31:04
Testi.sh run at 05/01/2018 10:31:26
$ cat /tmp/previews.txt(此行是由 preview-service.sh 启动脚本创建的,因此已运行。
File created 05/01/2018 09:26:44
********************************
Preview-service.sh run at 05/01/2018 10:31:26
tail -f -n 1000 /opt/monit/logfile 显示如下
[EET Jan 5 10:29:04] error : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
[EET Jan 5 10:29:04] info : 'phantomjs' exec: /opt/monit/bin/testi.sh
[EET Jan 5 10:30:04] error : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
[EET Jan 5 10:30:04] info : 'phantomjs' exec: /opt/monit/bin/testi.sh
[EET Jan 5 10:31:04] error : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
[EET Jan 5 10:31:04] info : 'phantomjs' exec: /opt/monit/bin/testi.sh
[EET Jan 5 10:32:04] error : 'phantomjs' '/opt/monit/bin/phantomjs-check.sh' failed with exit status (0) -- -= RiKKi!!!! =-
[EET Jan 5 10:32:04] info : 'phantomjs' exec: /opt/monit/bin/testi.sh
[EET Jan 5 10:33:04] info : 'phantomjs' status succeeded
当我在没有 sudo 的情况下将 testi.sh 脚本作为 spark 运行时,最后一个状态是成功的。
接下来我应该尝试什么?感谢我能得到的所有帮助!
【问题讨论】:
-
一般来说,在脚本中包含
sudo并不是一个好主意。改为使用sudo运行脚本本身。此外,尝试将行sudo bash /opt/previewservice/preview-service.sh start更改为sudo /opt/previewservice/preview-service.sh start。您正在使用sudo而不是脚本运行 bash。 -
我将 sudo bash 更改为 sudo 但也没有运气。
-
你为什么在脚本
testi.sh中使用#!/bin/sh而不是#!/bin/bash。根据您使用的发行版,这些解释器可能会有所不同。 -
我已经测试了 100 和 1 个不同的脚本,并对它们进行了一些调整并进行了测试。我不是“真正的”Linux 管理员(至少现在还不是),我主要使用 Ubuntu,但现在 CentOS 是我们的主要操作系统。这会影响所以我可以使用用户 spark 运行它们,但是当 Monit 运行脚本时它不能? (Monit 守护进程也作为 spark 启动)。
标签: linux bash shell monitoring monit