Process checking loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process checking loop
# 1  
Old 06-23-2011
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:
Code:
[#!/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
]

# 2  
Old 06-23-2011
This might help

code:
-----
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:
------
Code:
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.


Last edited by Franklin52; 06-24-2011 at 03:06 AM.. Reason: Please use code tags, thank you
# 3  
Old 06-23-2011
Thanks Jayan..i am able to get the desried results.
Thank you for the light thrown.
# 4  
Old 06-23-2011
Welcome .. !!
# 5  
Old 06-23-2011
I write a script for like issues more usefull than endless loopss Smilie
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
Code:
# ./process_run_check ntop 2
ntop process is not running.....
ntop process is not running.  
ntop process is running now with pid(s) 
26166

Code:
#!/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
Code:
# ./process_kill_check ntop 2
ntop(26340) process is running....
ntop is not running now !!

Code:
#!/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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop without checking file exists

In several scripts that process files matched by name pattern I needed to add a check for file existence. Just to illustrate let's say I need to process all N??? files: /tmp$ touch N100 N101 /tmp$ l ?10 -rw-rw-r-- 1 moss group 0 Apr 19 11:22 N100 -rw-rw-r-- 1 moss group ... (10 Replies)
Discussion started by: migurus
10 Replies

2. Shell Programming and Scripting

Checking DataPump Process

Hi All, I am writing script for Env refresh for Oracle DB. I am using Datapump for that. If i start expdp or impdp, how can i know that export or import has completed. I have query for that. How will i integrate with script?. Or any command i can run from Linux side. Please share you... (1 Reply)
Discussion started by: pvmanikandan
1 Replies

3. Shell Programming and Scripting

My Script For Process Checking is NOT Working

Hello there ULF, Good day! Just want to share my code and as well as my problem on why I'm not getting the output that I want. My original code was: #!/usr/bin/sh echo echo -n "Please input an IP-Pool: " read ip echo echo "Please wait....."... (8 Replies)
Discussion started by: rymnd_12345
8 Replies

4. Shell Programming and Scripting

block process checking

How can i check block process in Linux? If found any what action is required? How to check the pid of process? How to kill the block process? How to find out bottleneck process? (3 Replies)
Discussion started by: learnbash
3 Replies

5. Shell Programming and Scripting

while loop checking for file

Hi I my shell scripting(Main.ksh) i am calling the another loader.ksh using nohup inside the for loop ...so the above command runs parallel. Loader.ksh generate the dummy file seq_n.run file and deleted in the end of the Loader.ksh In the main.ksh .. after the for loop .. i have to check... (1 Reply)
Discussion started by: gavemani
1 Replies

6. UNIX for Dummies Questions & Answers

For loop for checking processes

Hi Gang, 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,... (8 Replies)
Discussion started by: jeffs42885
8 Replies

7. Shell Programming and Scripting

Checking for multiple instances of a process

Hi I have a scenario where i need to check multiple instances of a running shell script (abc.sh) . How can I find from inside a running shell script whether any other instance of the same script is running or not? If any other instance of same shell script is running I need to exit from... (4 Replies)
Discussion started by: raghu.amilineni
4 Replies

8. Shell Programming and Scripting

Checking the cron process in unix

Hi Experts, Is there any command by which i can chk that the cron process is running fine? Say i have scheduled the cron to run at 10 o clock every monday,Do i need to wait for the time it runs and then chk using ps -ef? Please shed some light. Thanks Ashok. (2 Replies)
Discussion started by: Ashok_oct22
2 Replies

9. Shell Programming and Scripting

Checking condition inside the loop

Hi all, I have one clarification i am using the loop which will process for each record .suppose there is f ailure in the first record it need to send mail and process the next .my code: defcount=`cat <filename>|wc -l` while ] do if <some condiotion> then echo "mail" fi done so... (1 Reply)
Discussion started by: ithirak17
1 Replies

10. HP-UX

checking process existed

On HP-UX, in application, if the process id has been get with the getpid() and sotred in database, then other monitor process want to check it if the process is existed, are there any system function can do it? I do not want to use the shell script, because it should use popen function to excuted... (5 Replies)
Discussion started by: Frank2004
5 Replies
Login or Register to Ask a Question