【问题标题】:monitor bash script execution using monit使用 monit 监控 bash 脚本执行
【发布时间】:2021-01-13 16:53:01
【问题描述】:

我们刚刚开始使用 monit 进行进程监控,并且在 monit 中使用了很多新功能。我在 /home/ubuntu/launch_example.sh 有一个 bash 脚本。这是连续运行的。是否可以使用 monit 进行监控?如果 bash 脚本终止,monit 应该启动脚本。应该是什么语法。我尝试了以下语法,但所有命令都没有以 ubuntu 用户身份执行,比如 shell 脚本调用了一些 python 脚本。

check process launch_example
matching "launch_example"
start program = "/bin/bash -c '/home/ubuntu/launch_example.sh'"
as uid ubuntu and gid ubuntu
stop program = "/bin/bash -c '/home/ubuntu/launch_example.sh'"
as uid ubuntu and gid ubuntu

【问题讨论】:

    标签: monit


    【解决方案1】:

    简单的答案是“不”。 Monit 仅用于监视,而不是某种主管/流程管理器。所以如果你想监控你长时间运行的可执行文件,你必须把它包装起来。

    check process launch_example with pidfile /run/launch.pid
      start program = "/bin/bash -c 'nohup /home/ubuntu/launch_example.sh &'"
        as uid ubuntu and gid ubuntu
      stop program = "/bin/bash -c 'kill $(cat /run/launch.pid)'"
        as uid ubuntu and gid ubuntu
    

    这种快速'n'dirty 的方式还需要为您的launch_example.sh 增加一行来编写pidfile(pidfile 匹配应该始终优先于字符串匹配)——它可能只是shebang 之后的第一行。它只是将当前进程 ID 写入 pidfile。这里没有什么花哨的;)

    echo $$ > /run/launch.pid
    

    事实上,将脚本转换为 systemd 单元并不难。 Here is an example on how to。然后可以通过 systemd 管理用户绑定、重新启动、pidfile 和“start-on-boot”(例如start program = "/usr/bin/systemctl start my_unit")。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 2010-11-16
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多