bash 监控重启程序脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27090373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:51:13  来源:igfitidea点击:

Monit restart program script

linuxbashshellmonit

提问by Zero

I am fairly new to monit and I was wondering if this script is enough to restart a crashed program lets say program1 is the name of the program.

我对 monit 还很陌生,我想知道这个脚本是否足以重新启动崩溃的程序让我们说 program1 是程序的名称。

check process program1
matching "program1"
start program = "/home/user/files/start.sh"
stop program = "/home/user/files/stop.sh"

Will it restart a crashed program now? And how can I assure it does not restart the program when it is working?

它现在会重新启动崩溃的程序吗?我如何确保它在工作时不会重新启动程序?

Edit: some more info The program uses port 30000 udp. Will this make it more cautious? And how many seconds are between the "cycles"?

编辑:更多信息 该程序使用端口 30000 udp。这会让它更加谨慎吗?“周期”之间有多少秒?

if failed port 30000 type UDP for 3 cycles then restart

回答by Зелёный

Monit uses the system call execv to execute a program or a script. This means that you cannot write shell commands directly in the start, stop or exec statements. To do this, you must do as above; start a shell and issue your commands there.

Monit 使用系统调用 execv 来执行程序或脚本。这意味着您不能直接在 start、stop 或 exec 语句中编写 shell 命令。为此,您必须按上述操作;启动一个 shell 并在那里发出你的命令。

Read about execution

Read about execution

This is just example what you should execute program or script:

这只是您应该执行程序或脚本的示例:

check process program1
matching "program1"
start program = "/bin/bash -c '/home/user/files/start.sh'"
stop program = "/bin/bash -c  '/home/user/files/stop.sh'"

Based on ConfigurationExamples

基于 ConfigurationExamples

回答by slashmili

let's say you have a script like this:

假设你有一个这样的脚本:

#!/usr/bin/python
import os, time
f = open('/tmp/myapp.pid', 'w')
f.write(str(os.getpid()))
f.close()
time.sleep(9)

Create a monit.conf

创建一个 monit.conf

set httpd port 2812 and allow monit:monit
set daemon 5
check process program with pidfile /tmp/myapp.pid
    start program = "/home/vagrant/myapp.py

Then run the monit with this command:

然后使用以下命令运行 monit:

monit -c monit.conf

Now Monit runs in the background and rerun the process if it dies

现在 Monit 在后台运行并在它死掉时重新运行该进程

回答by Juvenik

I will start a simple node server, which if you kill, monit is gonna restart it again and you will get an email too if set up correctly.

我将启动一个简单的节点服务器,如果你杀死它,monit 会重新启动它,如果设置正确,你也会收到一封电子邮件。

location /home/xxx/monitoring/nodejs

地点 /home/xxx/monitoring/nodejs

File: node-server.js

文件:node-server.js

var http = require('http');
var port = 8002;
var fs = require('fs');
var logStream = fs.createWriteStream(port+"_log.txt", {'flags':'a'});
var count = 0;
http.createServer(function(req, res){
    var output = (++count) + ' Request received in '+port+' @ ' + new Date()+'\n';
    console.log(output);
    logStream.write(output);
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    res.end('From '+port+' @ ' + new Date());
}).listen(port);
console.log('Server running @ ' + port)

FILE: server.sh

文件:server.sh

#!/bin/bash

process=
PID_FILE="/home/xxx/monitoring/nodejs/file.pid"
case $process in
    start)
        echo "STARTING node js server in port 8002"
        nohup /usr/sbin/node /home/xxx/monitoring/nodejs/node-server.js > /home/xxx/monitoring/nodejs/server.log 2>&1 &
        echo $! > $PID_FILE
        ;;

    stop)
        kill -9 $(cat $PID_FILE)
        rm $PID_FILE
        ;;

    *)
        echo "INVALID OPTION"
        ;;
esac

LOCATION: /etc/monit/monitrc (for ubuntu)

位置:/etc/monit/monitrc(对于 ubuntu)

set mail-format {
   from: monit@TEST
   subject: monit alert -- XXX-PROD $EVENT $SERVICE
   message: $EVENT Service $SERVICE
                 Date:        $DATE
                 Action:      $ACTION
                 Host:        $HOST
                 Description: $DESCRIPTION

Your faithful employee,

 }
set mailserver smtp.gmail.com port 587 username "[email protected]" password "xxx" using tlsv1
set alert [email protected]


check process nodejs-server with pidfile  /home/xxx/monitoring/nodejs/file.pid
        start program = "/home/xxx/monitoring/nodejs/server.sh start"
        stop program = "/home/xxx/monitoring/nodejs/server.sh stop

"

Once the server runs, hit the browser http://localhost:8002/. It will display some result. Now kill the process by finding its process id either from 'monit status' or from any other means. You will get a mail saying the process does not exists but again after some time, the server will run and you will get a mail saying 'process started again'.

服务器运行后,点击浏览器http://localhost:8002/。它会显示一些结果。现在通过从“监控状态”或任何其他方式查找进程 ID 来终止进程。您将收到一封邮件,说明该进程不存在,但一段时间后,服务器将再次运行,您将收到一封邮件,说明“进程再次启动”。

But please remember, if you stop the process from monit command like

但是请记住,如果您从 monit 命令中停止该进程,例如

monit stop nodejs-server

then it will not get restarted. And you get a mail saying that ur process has been stopped.

那么它不会重新启动。然后您收到一封邮件,说您的进程已停止。