check if job still alive and killing it after a certain walltime


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
check if job still alive and killing it after a certain walltime

Hi!

I'm using a script to start a process that might run forever if some parameters are given wrong (it's part of an optimization). I would now like to have the process killed after a certain walltime in that case. So far I get it done with the following lines

./My_process.e &
pid=`ps -ef | grep $$ | grep My_programme.e | grep -v grep | awk '{print $2}'`
sleep $Walltime
if kill -0 $pid 2>/dev/null;then
kill -TERM $pid
fi

This works but is not very convenient as the shell always is in sleep mode for $Walltime seconds, even if "My_process" runs properly and finishes after say 15 seconds. As I am calling this programme several hundred times and the walltime currently is 25 seconds it takes very long. It would be nice if the script would continue when the process is running properly and finishes by itself. I thought of something like "while sleep..." but can't figure it out....is there any way to do it like that or are there other nice ways?
Thankful for any hint,
ciwstevie
# 2  
A thought.

Use ps with -o lstart option. It is supposed to show you the start times. Get that time, and then see if 15 seconds has elapsed or not. You can avoid the sleep altogether.

Will require some scripting effort tho'.

Vino
# 3  
Probably the easiest way would be to pipe out the 'time' seconds from a date command (date +"%S") at execution of script, then run a check on that (+your time) with a count/do loop.

I'd do the script, but can't be arsed right now Smilie
# 4  
That's how it worked out

Here's the code that made it finally work....although I have to admit that it wasn't me who did it...thanks Steve Smilie

./My_process.e &
pid=`ps -ef | grep $$ | grep My_process.e | grep -v grep | awk '{print $2}'`
count=0
Walltime=30
while kill -0 $pid 2> /dev/null
do
sleep 1
count=`expr $count + 1`
if [ $count -gt $Walltime ] ; then
kill -TERM $pid 2> /dev/null
break
fi
done

Maybe somebody might make use of it as well....

Best regards,

CIWStevie
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Keep Server Alive with Cron Job
ajhalls
Despite my best efforts, my media streaming server still dies sometimes and I am in a random place trying to ssh into the server to restart it on my cell phone after customers start calling.... I tried using google to track down a script that would do the following Every 5 min execute: sudo...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
Check whether a process is alive or not
appleforme1415
Hi Everybody I have small requirement that needs to be implemented in shell script. Currently i have shell script which invokes a java process say "Process A" which runs in background. If some one tries to invoke again the same shell script , then there should be some mechanism inside the...... UNIX for Dummies Questions & Answers
23
UNIX for Dummies Questions & Answers
Killing an Xterm while leaving subprocess alive...
mschwage
Hi, I'm not quite understanding what I'm doing (happens often). This pseudocode works: #!/bin/pseudoksh function kill_parent { when i_want_to ; do sleep 2 kill -TERM $PPID exit done } kill_parent & ssh remote_host sh <<-EOF ...... Shell Programming and Scripting
0
Shell Programming and Scripting
killing a unix job after the job process gets completed
dtazv
Hi, Thanks in advance. i need to kill a unix background running job after that job process completes. i can kill a job by giving the following unix command kill -9 processid how to kill the job after the current process run gets completed ? Appreciate your valuable help. Thanks...... Solaris
7
Solaris
killing unix job after the job process completes
dtazv
Hi, Thanks in advance. i need to kill a unix background running job after that job process completes. i can kill a job by giving the following unix command kill -9 processid how to kill the job after the current process run gets completed ? Appreciate your valuable help. ...... Shell Programming and Scripting
1
Shell Programming and Scripting