How to kill a child script if it takes more than 10 minutes?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to kill a child script if it takes more than 10 minutes?
# 1  
Old 06-05-2013
How to kill a child script if it takes more than 10 minutes?

Hi all,
I have a query on killing a child process, if it takes more than 10 minutes

myparent.sh has the following
Code:
#!/bin/sh

echo "My Parent Script"
home/guru/initiateServer.sh


The initiateServer is a child process and this might take 20 or more minutes to return. I want to kill this process, if it is taking more than 10 minutes.

Please advice on what to do? Thanks all in advance

-Guru

Last edited by Franklin52; 06-05-2013 at 05:37 AM.. Reason: Please use code tags
# 2  
Old 06-05-2013
Hi guruincredible,

Your title had the answer, make it a child process and have the main process time the return of the child process.

Code:
#!/bin/sh

echo "My Parent Script"
home/guru/initiateServer.sh &
pid=$!
sleep 600
if [ kill -0 $pid ] {
 echo "killing initiateServer as it took too long to complete"
 kill $pid # you could optionally add a -9 in there if the server has a sigterm handler 
}

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 06-05-2013
What to do if it returns before 10 minutes?

Dear Skrynesaver,
thanks for the reply. if the child process returns before 10 minutes, say in 3 minutes, 7 minutes will be wasted.

Any way to move on if the child process is not alive, or if took more than 10 minutes, kill and move on?

Please advice.
# 4  
Old 06-05-2013
Good point, also had a cup of coffee and wrote if loop in shell syntax and caught kill -0 return correctly.

New improved scriipt, now with max wasted time of 10 seconds...
Code:
#!/bin/sh

echo "My Parent Script"
home/guru/initiateServer.sh &
pid=$!
for i in {0..60} ; do
   kill -0 $pid
   if [ $? -eq 1 ]  ; then
      exit
   fi
   sleep 10
done
kill $pid

Alternatively if this is a subroutine in a larger script use return rather than exit.
# 5  
Old 06-05-2013
How about shedule kill command using at ?
Code:
 
#!/bin/sh
echo "My Parent Script"
home/guru/initiateServer.sh &
pid=$!
at now + 10 minutes <<_EOF_
kill $pid 2>/dev/null
_EOF_

# 6  
Old 06-05-2013
Dear pravin27,

You risk either killing an innocent process that happens to have been created later on with the same process id (especially if you are running as the super-user) but you will probably be generating mail. I would go with Skrynesaver's suggestion. Far safer. It might be worth investing a little time in listing the process details at the start and at the end to ensure you are definitely killing the same process.




Robin
Liverpool/Blackburn
UK
# 7  
Old 06-05-2013
I have solved this a year ago:
Code:
home/guru/initiateServer.sh &
task=$!
( sleep 600 && kill $task ) </dev/null >/dev/null 2>&1 &
watchdog=$!
wait $task
sleep 1
kill $watchdog >/dev/null 2>&1

This gives a 99% safety that no innocent process is killed.
The 100% method is with perl:
Code:
perl -e "alarm 600; exec @ARGV" home/guru/initiateServer.sh

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Ldapsearch takes minutes when using FQDN vs IP

Hey All, ldapsearch takes minutes when using FQDN vs IP. What could be some of the reasons for that? Cheers, DH (13 Replies)
Discussion started by: Devyn
13 Replies

2. Shell Programming and Scripting

Find the Pid and Kill the Process after a Few Minutes

hi guys i had written a shell script Display Information of all the File Systems i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it??? sample.sh df -a >> /tmp/size.log and my cron to execute every minute every hour every day * *... (5 Replies)
Discussion started by: azherkn3
5 Replies

3. Shell Programming and Scripting

Kill child processes when exit

Hi, I have parent script which is invoking multiple child scripts. I would want to kill all the child processes before the parent process exit. > cat ./parent #!/bin/ksh while do . ./child arg1 & if ; then break fi done Is there a way to get the process group id for all the child... (3 Replies)
Discussion started by: midhun19
3 Replies

4. Shell Programming and Scripting

Kill all child process of a script

Hi guys i have a problem with a script... this script creates differents GUI with YAD... well i want that when i press the "Cancel" button on this graphical interface all the child process and even the same script should be killed #!/bin/bash function gui_start { local choice="" ... (4 Replies)
Discussion started by: maaaaarco
4 Replies

5. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

6. UNIX for Advanced & Expert Users

Kill all child processes on trap

hi OS: Sun Solaris I have a scenario that when someone presses ctrl-c while executing a shell script, it should not just exit. it should kill all the child processes started by the running shell script only. I am executing many other scripts parallely which in turn fork off more... (2 Replies)
Discussion started by: rakeshou
2 Replies

7. Shell Programming and Scripting

script to kill rsh processes running for more than 10 minutes

Hi Friends, I need to write a script to kill some processes running for more than 10 minutes. Can I get some pointers on that. Thanks for ur help in Advance. Thanks&Regards, Amit (1 Reply)
Discussion started by: amitsayshii
1 Replies

8. UNIX for Advanced & Expert Users

script to kill rsh processes running for more than 10 minutes

Hi Friends, I need to write a script to kill some processes running for more than 10 minutes. Can I get some pointers on that. Thanks for ur help in Advance. Thanks&Regards, Amit (1 Reply)
Discussion started by: amitsayshii
1 Replies

9. UNIX for Dummies Questions & Answers

kill parent and child

Hello all, I have gone through the search and looked at posting about idle users and killing processes. Here is my question I would like to kill an idle user ( which I can do) but how can I asure that all of his process is also killed whit out tracing his inital start PID. I have tried this on a... (4 Replies)
Discussion started by: larry
4 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question