Automatically monitor and restart Linux processes using monit without a process ID pid file

On some of the servers that I maintain, I use monit to monitor the status of key system services. I have it set up to try and automatically restart the service and only notify me if it’s unable to restart it. This helps to keep everything running smoothly without manual intervention.

As part of its configuration, monit needs a way of knowing whether each service is actually running. (This is in addition to checking that the service is responding to requests, for example a web server listening on port 80/443.) This is done by means of checking the process’s ID (pid) in the process list. In the past I’ve always done this by means of the pid file, which is usually a plain text file /run/service.pid containing the process pid that is automatically created when it starts up. More recently, on Debian some services (notable postgrey) have stopped creating a pidfile when they start up. Unless I created one manually, monit therefore thought that the service wasn’t running, and would stop trying to monitor it. Another approach was needed.

It turns out that in newer versions of monit, there is a feature to search for the process by name rather than specifying its pid. Here’s a sample config for postgrey:

#check process postgrey with pidfile /run/postgrey.pid
check process postgrey matching postgrey
  start program  = "/bin/systemctl start postgrey"
  stop program  = "/bin/systemctl stop postgrey"
  if failed port 10023 type tcp then restart
  if 5 restarts within 5 cycles then timeout

The original process existence check is commented out at the top and has been replaced with the newer one. The rest of the configuration is unchanged. This snippet needs to be included in the config file at /etc/monit/monitrc, or better yet, in a custom include file such as /etc/monit/conf-enabled/postgrey (so that it will not cause problems with package updates).

References:

Add new comment

CAPTCHA