Monit脚本无法重新启动服务 [英] Monit script not working to restart service

查看:69
本文介绍了Monit脚本无法重新启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以请耐心等待我!

This is my first post so please be patient with me!

我尝试创建脚本来检查服务是否无法访问(http错误代码),然后Monit应该重新启动程序(预览服务).Monit以"spark"用户身份运行.

I have tried to create script that checks if service is unreachable (http error code), then Monit should restart program (Preview Service). Monit is run as user "spark".

这是phantomjs-check.sh代码:

This is phantomjs-check.sh code:

#!/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,这是应该的工作方式.

If I manually kill previewservice and run that script I get exit code of 0 which is how that should work.

在Monit中,我有以下conf:

In Monit I have following conf:

check program phantomjs with path "/opt/monit/bin/phantomjs-check.sh"
if status = 0 then exec "/opt/monit/bin/testi.sh"

当前,我向其中添加了一些日志记录,这是test.sh代码:

Currently I added some logging to it and this is test.sh code:

#!/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文件中,我有一行:

In /etc/sudoers file I have line:

spark ALL=(ALL) NOPASSWD: /opt/previewservice/preview-service.sh

此命令可从cli运行,并启动/重新启动Previewservice.我可以手动运行"testi.sh"脚本作为spark [spark @ s-preview-1 bin] $ ./testi.sh ,它可以按预期工作,但是即使Monit也会得到该服务已关闭的信息它没有开始.

This command works from cli and it starts/restarts previewservice. I can run "testi.sh" script manually as spark [spark@s-preview-1 bin]$ ./testi.sh and it works as intended, but even Monit gets info that service is down it doesn't start.

$ 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启动脚本创建的,因此已运行.

$ cat /tmp/previews.txt (This line was created by preview-service.sh start script so it has been run.

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

最后一个成功的状态是当我不打扰地将testi.sh脚本作为spark运行时.

And the last status succeeded comes when I run that testi.sh script as spark without sudoing.

有什么提示我接下来应该尝试什么?我感谢我能获得的所有帮助!

Any tips what I should try next? I appreciate all the help I can get!

推荐答案

Monit通常以 root 用户身份运行.是你的情况吗?如果是,则可能不需要sudo部分.

Monit is usually running at root user. Is it your case ? If yes, you probably don't need the sudo part.

考虑到脚本在Monit之外而不是在Monit之外工作之后,Monit具有自己的 PATH 环境变量,该变量非常小.建议将脚本/binairies的完整路径写为:

After regarding you script working outside of Monit but not from Monit, Monit is having its own PATH environment variable which is very small. It is recommended to write full path to your script/binairies as:

/usr/bin/sudo /bin/bash /opt/previewservice/preview-service.sh start

这篇关于Monit脚本无法重新启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆