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?
# 8  
Old 06-05-2013
Hi.

See also:
Code:
NAME
       timeout - run command with bounded time

SYNOPSIS
       timeout [-signal] time command ...

DESCRIPTION
       timeout  executes  a  command  and  imposes an elapsed time limit.  The
       command is run in a separate POSIX process  group  so  that  the  right
       thing happens with commands that spawn child processes.

and
Code:
NAME
     timelimit - effectively limit the absolute execution time of a process

SYNOPSIS
     timelimit [-q] [-S killsig] [-s warnsig] [-T killtime] [-t warntime]
               command [arguments ...]

DESCRIPTION
     The timelimit utility executes a given command with the supplied
     arguments and terminates the spawned process after a given time with a
     given signal.  If the process exits before the time limit has elapsed,
     timelimit will silently exit, too.

Both were in the Debian repository, there may be others as well.

The versions I have are:
Code:
timeout - ( /usr/bin/timeout, 2008-04-29 )
timelimit - ( /usr/bin/timelimit, 2008-07-18 )

The latter can also be found at timelimit — limit a process's absolute execution time

Best wishes ... cheers, drl
# 9  
Old 06-05-2013
Quote:
Originally Posted by MadeInGermany
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

The following is POSIX-compatible and never signals an "innocent process":
Code:
set -m
sh -c '(/path/to/task; kill 0) & (sleep 600; kill 0) &' &

A more readable alternative:
Code:
set -m
sh <<'EOF' &
    (/path/to/task; kill 0) &
    (sleep 600; kill 0) &
EOF

If it should wait:
Code:
set -m
sh <<'EOF' &
    (/path/to/task; kill 0) &
    p=$!
    (sleep 600; kill 0) &
    wait
EOF
wait

set -m ensures that the non-interactive background shell (and all of its children) which does all the work is placed in its own, dedicated process group, regardless of whether the main script is invoked from an interactive shell or not. From within that process group, any member can signal all other members without affecting outsiders. Even if a pid is reused, it will not be signaled because it will no longer be a member of the process group.

Naturally, stdout and stderr should likely be redirected.

Regards,
Alister

Last edited by alister; 06-06-2013 at 05:23 AM.. Reason: bug fix and typo
These 2 Users Gave Thanks to alister For This Post:
# 10  
Old 06-06-2013
Thanks, it's even in man sh: kill 0 kills the "process group". (I thought it only kills itself.)
And works with a traditional Unix shell, too.

---------- Post updated at 07:26 AM ---------- Previous update was at 06:15 AM ----------

I just checked my old script - it uses this in a loop, so kill 0 cannot be used.
# 11  
Old 06-06-2013
I've made it work on AIX (4.3.3, 5.1 & 6.1) , HP-UX (11.11i) and RHEL (6.3)

I saved and ran this demo:-
Code:
set -m
date
sh <<'EOF' &
    (ping -c 50 localhost ; kill 0) &
    p=$!
    (sleep 6; kill 0) &
    wait
EOF
wait
date

I got the output:-
Code:
$ script_timeout_demo
Thu  6 Jun 13:59:05 2013
PING loopback (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=255 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=255 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=255 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=255 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=255 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=255 time=0 ms
Terminated
Thu  6 Jun 13:59:11 2013
$

I had to change the ping command on HP-UX, because that syntax is ping localhost -n 50

Some give variations on the Terminated message, but that can be handled/ignored/re-directed.

So, alister - absolutely fantastic. You should see some of the rubbish I've coded in the past to try to do the same sort of thing. I regret that I can only thank a post once.


Robin
This User Gave Thanks to rbatte1 For This Post:
# 12  
Old 06-06-2013
Quote:
Originally Posted by MadeInGermany
I just checked my old script - it uses this in a loop, so kill 0 cannot be used.
Could you please post your code, or a simplified yet fully-functional structural equivalent?

Your "99%" solution is probably more like a "99.9999%" solution. It's extremely unlikely that you need anything better. However, I'm interested to see if yours is a situation in which I cannot make my approach work.

So long as the only things in the process group are the monitored and monitoring processes, I don't see why kill 0 cannot be used.

Regards,
Alister

---------- Post updated at 04:53 PM ---------- Previous update was at 04:44 PM ----------

Quote:
Originally Posted by rbatte1
I've made it work on AIX (4.3.3, 5.1 & 6.1) , HP-UX (11.11i) and RHEL (6.3)

...<snip>...

So, alister - absolutely fantastic. You should see some of the rubbish I've coded in the past to try to do the same sort of thing. I regret that I can only thank a post once.
Regarding rubbish code, we've all been there. I have actually never used this code; it is something I come up with yesterday while pondering this thread and the possibility of a portable solution without a pid race. For that reason I appreciate your multi-platform testing. Thank you very much.

Regards,
Alister
This User Gave Thanks to alister 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