A services watchdog written in Perl

This Perl script was initially written about 3 or 4 years ago, but still does its daily job: monitor that certain processes are running and if not, restart them (plus send out a notification).

The script should run as the root user (since it will be restarting services or processes), and can easily be configured as a cron job.

Let’s say you’ve saved your script in /root/bin/services_watchdog.pl, then the cronjob could look like:

This will check every 5 minutes if all required processes are running.

The script itself will start with:

The script uses Proc::ProcessTable to retrieve a full process list.
The processes it should monitor, are easily defined in a hash:

The actual processing of the process list can be done in a minumum of lines:

Finally, the script tries to restart the missing service and prints a log message:

And that’s all folks!

Comments are closed.