Monit script restart program

I am new to monit, and I was wondering if this script is enough to restart a broken program, 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 he restart the broken program now? And how can I assure that it does not restart the program when it works?

Edit: Additional information The program uses the 30000 udp port. Will this make him more careful? And how many seconds between "cycles"?

 if failed port 30000 type UDP for 3 cycles then restart 
+6
source share
3 answers

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

Read about execution

This is just an example of what you should execute, or a 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

+7
source

let's say you have a script as follows:

 #!/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 file

 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 monit with this command:

 monit -c monit.conf 

Monit now runs in the background and restarts the process if it dies

+1
source

I started a simple node server which, if you kill, monit will restart it again and you will receive an email if it is configured 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 

"

After starting the server, click the browser http: // localhost: 8002 / . He will show some result. Now kill the process by finding its process identifier either from the "monit" state or from any other means. You will receive a message that the process does not exist, but after a while the server will start, and you will receive a message that "the process was started again."

But remember, if you stop the process from the monit command, for example

 monit stop nodejs-server 

then it will not be restarted. And you get a message that the ur process has been stopped.

0
source

Source: https://habr.com/ru/post/978576/


All Articles