Monit restart program script

17,683

Solution 1

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

Solution 2

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

Share:
17,683
Zero
Author by

Zero

Updated on June 18, 2022

Comments

  • Zero
    Zero almost 2 years

    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