Process check script for linux

If you ever wanted an automation when a process is not running it is just a small script helping to achieve this:

check_process.sh (dont forget to make it executable)

check_process() {
  echo "$ts: checking $1"
  [ "$1" = "" ]  && return 0
  [ `pgrep -f $1` ] && return 1 || return 0 
#pgrep -n if process-name match exactly
}

while [ 1 ]; do
  # timestamp
  ts=`date +%T`
  echo "$ts: begin checking..."
  check_process "myprog.pl"
  [ $? -eq 0 ] && echo "$ts: not running, restarting..." && `/usr/bin/perl /home/me/myprog.pl >/dev/null`
sleep 10

done

You can also send yourself a mail instead of just starting process instead of

/usr/bin/perl /home/me/myprog.pl

do

echo "Subject: Process is stopped" | sendmail yourmail@mail.com

If you want to run this in background do

nohup check_process.sh &

or you can put this on startup via cronjob

crontab -e

@reboot root /home/me/check_process.sh

Origin/Source: https://stackoverflow.com/questions/7708715/check-if-program-is-running-with-bash-shell-script

2+

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.