Monday, October 18, 2010

Shell Scripting and Infinite possibilties

To check a server is alive or not we simply ping that system, Similarly to check a process is alive or not (eg .running in infinite loop) we need some utilities. I hope this simple script might help you.

#!/bin/sh
# Written By: Pawan
# Last updated:YYYY-MM-DD

#set -x ### uncomment this line to enable debugging

## Script simply checks health of job running in infinite loop it might be your java, shell, python or any other job

jobs=( "abc.sh" "/home/user/cde.py" ) ## write your jobs those health is to be checked seperated by single space with absolute path or file name if same directory

## checking health of each job in loop

while [ "${jobs[${i}]}" != "" ]
do
loop_pid=`ps -ef | grep ${jobs[${i}]} | grep -v 'grep' | awk '{print $2}'`
then
echo "starting ${jobs[${i}]} if not running"
sh $job_path/${jobs[${i}]} &  ## "&" Will start your job in backend
else
echo "${jobs[${i}]} already running..."
fi
let i=i+1
done