Linux – Monitor a process using monit in ubuntu

linuxmonitmonitoringscripting

On my server I have started a service using a script created by myself.
I want to monitor that process using monit. I didn't see any .pid file for that process in the /var/run directory. How can I monitor that process using monit?

Best Answer

Remember, it is possible to use Monit to manage a process that does not have a PID file. In newer versions of the utility (which you should have) you can leverage the matching directive to check the name of your script. If it's running, try using the monit procmatch yourscriptname command, i.e.:

# monit procmatch orca
List of processes matching pattern "orca":
------------------------------------------
    /usr/bin/perl -w # -*- perl -*- /usr/local/bin/orca -d procallator.cfg
------------------------------------------
Total matches: 1

Keep in mind that monit only tracks the first instance it finds using the procmatch filter.
Your monit code would look something like this:

check process myprocessname
        matching "myprocessname"
        start program = "/etc/init.d/myproccessname start"
        stop program = "/usr/bin/killall myprocessname"
        if cpu usage > 95% for 10 cycles then restart

Also see: monit: check process without pidfile