0

What's good "ghetto process monitor" without installing any additional software besides cron and some standard UNIX tools? I.e. check in a shell script if a certain process has died and been dead for a while to restart it? This would be to restart segfaulting Varnish process.

Mikko Ohtamaa
  • 1,374
  • 3
  • 17
  • 28

1 Answers1

2

You can create a simple bash script running every minute and checking whether the process is running. If not, it can create file and in one of the next runs according to the file creation date it can make a decision whether to start the varnish process again or not.

Simple example:

#!/bin/bash
pid=$(ps -ewwo args | grep [v]arnish)
check_file=/var/run/varnish.checker

# minimum difference in seconds
min_difference=180

if [ -f "$check_file" ]
then
    difference=$(($(date +"%s") - $(stat -c %Y "$check_file")))
    if [ $difference -gt $min_difference ]
    then
        ### start the varnish process here ###
        rm -f "$check_file"
    fi
else
    if [ -z $pid ]
    then
        touch "$check_file"
    fi
fi

But why you doesn't want to use external utility for this? Monit is a great tool (daemon) for this stuff. Also it can monitor many other processes.

Viktor Stískala
  • 413
  • 3
  • 10