Omnipresent IT Solutions Pvt Ltd

Saturday, January 7, 2012

Shell script to check if a process is running, if not start it.

Shell script to check if a process is running, if not start it.

We have 2 monitoring process which will check each other as well as the process

#!/bin/bash
#monitor.sh
#This process will monitor 1.Process is running
#
2.Other monitor process is running
#Sleeping time is 20s
myhome=/home/rset/Desktop
sleep 10
while :
do
sleep 20
#Process checking
#ps -ef | grep -v grep | grep process.sh
if ps -ef | grep -v grep | grep process.sh > /dev/null
then
echo "Running" > /dev/null
myprocessid=`pgrep process.sh`
echo "###########PROCESS STATUS#############" >> $myhome/procstats.log
cat /proc/$myprocessid/status > $myhome/procstats.log
echo "###########INSTRUCTION POINTER#############" >> $myhome/procstats.log
ps -o eip $myprocessid >> $myhome/procstats.log
echo "###########" >> $myhome/procstats.log
else
echo "Not Running"
#Start it
echo "Starting"
cd $myhome
./process.sh
echo "killed"
fi
if ps -ef | grep -v grep | grep monitorslave.sh > /dev/null
then
echo "Monitor slave already running" > /dev/null
else
echo "Monitor slave started"
cd $myhome
./monitorslave.sh
fi

done

#!/bin/bash
#monitorslave.sh
#This process will monitor 1.Process is running
#
2.Other monitor process is running
#Sleeping time is 20s
myhome=/home/rset/Desktop
while :
do
sleep 20
#Process checking
#ps -ef | grep -v grep | grep process.sh
if ps -ef | grep -v grep | grep process.sh > /dev/null
then
echo "Running" > /dev/null
myprocessid=`pgrep process.sh`
echo "###########PROCESS STATUS#############" >> $myhome/procstats_slave.log
cat /proc/$myprocessid/status > $myhome/procstats_slave.log
echo "###########INSTRUCTION POINTER#############" >> $myhome/procstats_slave.log
ps -o eip $myprocessid >> $myhome/procstats_slave.log
echo "###########" >> $myhome/procstats_slave.log
else
echo "Not Running"
#Start it
echo "Starting"
cd $myhome
./process.sh
echo "killed"
fi
if ps -ef | grep -v grep | grep monitor.sh > /dev/null
then
echo "Monitor already running" > /dev/null

File: /home/rset/Desktop/procesim

else
echo "Monitor started"
cd $myhome
./monitor.sh
fi

done

#!/bin/bash
#process.sh
#Process to print from up to 5000
i=`tail -1 process.log`
j=5000
while [[ $i -le $j ]]
do
sleep 2
echo $i >> process.log
echo $i
i=`expr $i + 1`
done

No comments: