I need part of my script to be able to loop, check for processes running and if they aren't running, start them. It needs to loop 5 times, do a check each time, and make sure a process starts, and if its running; skip it.
I've worked with loops and checking for processes before, but this one has me stumped.
now=$(date + %u%H)
for i in 1 2 3 4 5
do
process=$(ps -ef | grep process | awk '{ print $2}')
if [ -n "$process" ]
then
process1=$(ps -ef | grep process1 | awk '{ print $2}')
if [ -n "$process1" ]
then
if [ $now -gt 100 ] || [ $now -lt 108] # If sunday between 12 and 8
cd /dir/startprocess1.sh
cd /dir/startprocess2.sh
cd /dir/startprocess3.sh
else
cd /dir/startprocess1.sh
cd /dir/startprocess2.sh
cd /dir/startprocess3.sh
startcustomprocess1.sh
startcustomprocess2.sh
fi
else
cd /home/dir/process1.sh
sleep 10
if [ $now -gt 100 ] || [ $now -lt 108] # If sunday between 12 and 8
cd /dir/startprocess1.sh
cd /dir/startprocess2.sh
cd /dir/startprocess3.sh
else
cd /dir/startprocess1.sh
cd /dir/startprocess2.sh
cd /dir/startprocess3.sh
startcustomprocess1.sh
startcustomprocess2.sh
fi
fi
else
mail -s "Process isnt running" me@email.com
sleep 15
fi
done
now=$(date +%u%H) # no space between + and what follows
---------- Post updated at 17:08 ---------- Previous update was at 16:50 ----------
cd /dir/startprocess1.sh
cd /dir/startprocess2.sh
cd /dir/startprocess3.sh
Im missing something here: Are you changing 3 time of directory (if so wht is the point?), or is there something missing ( cd; or cd dir1; startprocess1.sh)?
Hi Jeff,
provide me the processes that should run every day and put it in a file say file 1
and find the current that would compare the current processes alive and put the output 2 file say file 2
use diff command to find those processes not running .
and restart those process .
also to check for loops ,
if you require you can create another script which wuld run the above script with while condition and that condition which will always gets satisfied. say
#!/bin/bash
a=10
while [ "$a" -eq "10" ];
do
run the above script
sleep (put the min u want to check)
done
You might have trouble with using ps & grep, because sometimes
ps -ef|grep something
will find a processes "grep something", so you can get spurious errors.
You would be safer using a regular expression in the grep, even if there is nothing to expand. Something like:-
ps -ef|grep somethin[g]
will never match the grep statement itself. Apart from that, you code looks okay, but it could be simplified. If I may suggest:
proc1=$(ps -ef | grep -c proc[1])
proc2=$(ps -ef | grep -c proc[2])
if [ $proc1 -eq 0 -o $proc2 -eq 0 ]
then
....whatever....
fi
Of course, if you logic is that if neither process is running, you would want an AND condition, so a -a flag in the if statement. Of course, if that is the case, you can save some processing by combining the first two lines, so:
procs=$(ps -ef | egrep -c "proc[12]")
if [ $proc1 -eq 0 -o $proc2 -eq 0 ]
then
....whatever....
fi
I hope that this is useful, but if i have missed the point, please write back and hopefully I or someone else will correct it.
Hi Robin,
The above script is good but i wld get only the count of the process that are not running however not which process that should run or how we will find the missing processes.
Robin do correct me with this,if am wrong.