简体   繁体   中英

Monit restart program script

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.

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"?

if failed port 30000 type UDP for 3 cycles then restart

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.

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

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

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 -c monit.conf

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

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.

location /home/xxx/monitoring/nodejs

File: 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

#!/bin/bash

process=$1
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)

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 "services.xxx@gmail.com" password "xxx" using tlsv1
set alert xxx@gmail.com


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'.

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

monit stop nodejs-server

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM