Process checking loop

Hi,

I want to create a script who will check if the java process is running & if it finds the process is still there it continues to execute & when the process completes it exit from the script.

I have written a code to check & notify the process existence but i am not getting how to write the logic for continuation & exiting code between this .

Please help

Here is the code i have written:

[#!/bin/ksh
pid=""
pid='ps -ef | grep "<process string>" | awk ' {print $2}''
echo $pid
if [ "$pid"="" ]
then
echo "process not running"
else
echo service is ok
pid=""
fi
]

This might help

code:
-----

while [ 1 ]
do
        pid=`ps -ef | grep "<process_name>" | grep -v grep | awk ' {print $2}'`
        echo $pid
        if [ "$pid"="" ]
        then
                echo "Process is not there... Hence exiting..."
                exit
        else
                echo "process is running ... hence continuing..."
        fi
        sleep 3
done

Output:
------

If the process is not running, then it will quit the loop . If it is running, then will go sleep for 3 seconds and try it again.

Thanks Jayan..i am able to get the desried results.
Thank you for the light thrown.

Welcome .. !!

I write a script for like issues more usefull than endless loopss :slight_smile:
if you some process is running and you want to control for end of(kill) status in specific times
## below for ( control counts * interval len) = 2*5 =10 second check
time (interval len can be changable in script with len parameter)

for example
# your process is not running and you want to watch for run

# ./process_run_check ntop 2
ntop process is not running.....
ntop process is not running.  
ntop process is running now with pid(s) 
26166
#!/bin/ksh
## justdoit process "run" check @unix.com ##
process=$1;x='.';len=5;c=$2;cc=$c;f=1
write_msg() {
echo "e.g.. --> ' $0 java 10 '" ; exit 1
}
if [ ! $# -eq 2 ] ; then write_msg;fi
if [[ ! $(echo $2|grep -w [[:digit:]]*) ]] ; then echo "second parameter '$2' is not digit!!";write_msg;fi
check_process() {
if [[ $(eval $pgrepx) ]] && [ $1 -eq 1 ] ; then
printf "%s%s\n%s\n" "$process" "process is already running with pid(s)" "$(eval $pgrepx|awk '{print '$write'}')" ; exit 0
elif [[ $(eval $pgrepx) ]] ; then
printf "\n%s%s\n%s\n" "$process process is running now with pid(s)" "$(eval "$pgrepx"|awk '{print '$write'}')" ; exit 0
fi
}
check_pgrep() {
if [ ! $(rpm -qa|grep procps) ] ; then
pgrepx="ps -ef|grep "$1"|grep -v grep|grep -v $$";write=\$2
else
pgrepx="pgrep $process";write=\$1;
fi
}
check_pgrep $process
check_process $f;
while [[ ! $(eval $pgrepx) ]] && [ ! $c -eq 0 ] ; do
((f++)) ; printf "%s %s" "$process" "process is not running" ; for((i=0;i<$len;i++)); do printf "%s" "$x" ; sleep 1
check_process $f; done;echo;((c--));done
echo "$process process is not running after $cc checks"

other script will allow that you want thing control so
# your process is running and you want to watch for terminate

# ./process_kill_check ntop 2
ntop(26340) process is running....
ntop is not running now !!
#!/bin/ksh
## justdoit process "kill" status check @unix.com ##
process=$1;x='.';len=5;c=$2;cc=$c;f=1
write_msg() {
echo "e.g.. --> ' $0 java 10 '" ; exit 1
}
if [ ! $# -eq 2 ] ; then write_msg;fi
if [[ ! $(echo $2|grep -w [[:digit:]]*) ]] ; then echo "second parameter '$2' is not digit!!";write_msg;fi
check_process() {
if [[ ! $(eval $pgrepx) ]] && [ $1 -eq 1 ] ; then
printf "%s %s\n" "$process" "process is not running !! " ; exit 0
elif [[ ! $(eval $pgrepx) ]] ; then
printf "\n%s %s\n" "$process is not running now !!" ; exit 0
fi
}
check_pgrep() {
if [ ! $(rpm -qa|grep procps) ] ; then
pgrepx="ps -ef|grep "$1"|grep -v grep|grep -v $$";write=\$2
else
pgrepx="pgrep $process";write=\$1;
fi
}
check_pgrep $process
check_process $f;
while [[ $(eval $pgrepx) ]] && [ ! $c -eq 0 ] ; do
((f++)) ; printf "%s %s" "$process("$(eval "$pgrepx"|awk '{print '$write'}')\) " process is running" ; 
for((i=0;i<$len;i++)); do printf "%s" "$x" ; sleep 1
check_process $f; done;echo;((c--));done
echo "$process process is still running after $cc checks"

regards
ygemici