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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if job still alive and killing it after a certain walltime
# 1  
Old 07-26-2005
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  
Old 07-26-2005
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  
Old 07-26-2005
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  
Old 07-27-2005
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Check the URL is alive or dead with port number

Hi, I am running certain weblogic instance, in which it's hard to find which instance is stopped or running after stopping the weblogic , cause process status is for all the instance is same. A URL which shows the instance is stopped or running. But i have major challenge to check it through... (2 Replies)
Discussion started by: posix
2 Replies

2. UNIX for Advanced & Expert Users

Check the status of job

Hi, I have master job which will run based on the sub jobs status. In the master job I am giving the condition like, condition: s(sub_job) f(sub_job) This scenario will work if the sub job status is success or failed. but I want to run my master job even if the sub_job was... (1 Reply)
Discussion started by: Kattoor
1 Replies

3. UNIX for Advanced & Expert Users

Issue while killing the process using autosys job

Hi, I have one autosys job that will retrieve the proccess id's and will kill those processess as follows, pid=`/usr/ucb/ps -auwwxx | grep MAIN |nawk '{print $2}'` kill -9 pid but after executing this particular job, its status is showing as TE(terminated) and the kill process is... (3 Replies)
Discussion started by: Kattoor
3 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripts - Killing a job....

Hello all, I need to write a shell script that does the following; Allows you to kill a job,(1) listing only the jobs you own, (2) asks for which job to kill, (3) kills the job and (4) confirms kill... I am not sure if I need to first run the job command and pipe it with kill? Which options... (6 Replies)
Discussion started by: citizencro
6 Replies

5. UNIX for Dummies Questions & Answers

Keep Server Alive with Cron Job

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... (1 Reply)
Discussion started by: ajhalls
1 Replies

6. Shell Programming and Scripting

How to check if a pid is alive?

I want to do some operations provided the pid is active. (6 Replies)
Discussion started by: ScriptDummy
6 Replies

7. UNIX for Dummies Questions & Answers

Check whether a process is alive or not

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... (23 Replies)
Discussion started by: appleforme1415
23 Replies

8. Shell Programming and Scripting

Killing an Xterm while leaving subprocess alive...

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 ... (0 Replies)
Discussion started by: mschwage
0 Replies

9. Solaris

killing a unix job after the job process gets completed

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... (7 Replies)
Discussion started by: dtazv
7 Replies

10. Shell Programming and Scripting

killing unix job after the job process completes

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. ... (1 Reply)
Discussion started by: dtazv
1 Replies
Login or Register to Ask a Question