On Saturday 07 April 2007, Michael Velez wrote:
-----Original Message----- From: centos-bounces@centos.org [mailto:centos-bounces@centos.org] On Behalf Of Peter Gross Sent: Saturday, April 07, 2007 12:06 AM To: CentOS mailing list Subject: Re: [CentOS] command to ensure other command does last longer than5 seconds
Jerry Geis wrote:
Hi,
I am wondering if there exists a centos command that runs another command and ensures the second command doesnt take more than x seconds? When x is given on the command line.
If the second command is not "done" the first command will
just kill
it and both exit.
Does such a method or command exist?
I just need to ensure the second command does just continue
to run and
run and run.
Here's my admittedly kludgey quick and dirty way of doing this .... write a shell script that does the following:
- takes two arguments -- the command to run (in quotes) and
then the drip dead time (in seconds? or minutes?)
- start the command in the background, saving its PID in a
var (say $pid).
- create an "at" job to kill the pid at the appointed time, as in:
echo kill -TERM $pid | at now + 15 minutes
If the job has already finished, the kill -TERM will hopefully be harmless (i.e., the pid's haven't cycled around and there is now a new, but different, job with the same pid). _______________________________________________
I think the best way to do it would be with the sleep command since the 'at' command does not allow you to specify seconds.
In a script (which I presume is your first command) start the second command in the background, get the pid of that second command, then sleep for 5 seconds, and kill it.
Michael
Just building off Micheal's idea:
killafter.sh <command> <time> #!/bin/bash
$1 & pid=$! sleep $2 kill -TERM $pid