Running a shell command for a specific period of time

Bill Warner wwarner42 at cox.net
Wed Oct 26 13:34:02 MST 2005


Yea, I'd say you're pretty limited to:

------------------------------------
dosomething &
pid=$!
sleep $x
kill $pid
------------------------------------

You could put a small amount of logic to check it every once in a while
as well.

Maybe
------------------------------------
dosomething &
pid=$!

while [ $miutes < $timeout ]; do
	sleep 60
	let minutes=minutes+1
	stillrunning=`ps -ef |grep $pid` # wont work if new proc is started and
gets same pid
	if [ ! -z $still ]; then
		break;
	fi
done
-------------------------------------

*disclaimer i didn't test or even try to run any of this.



On Wed, 2005-10-26 at 12:51 -0700, Erik Bixby wrote:
> So, I'm guessing there's no elegant way of doing what I want,
> something similar to "time" that calls a command and acts upon it,
> somehow...
> -Erik
> 
> On 10/26/05, vodhner at cox.net <vodhner at cox.net> wrote:
>         Erik wrote:
>         > I was wondering if anyone knew a way to allow a shell
>         > command to run for a specific period of time.  In this
>         > particular case, I want tcpdump to run for 23 hours,
>         > 59 minutes, 59 seconds. 
>         
>         It would be best if the process could limit itself, since
>         anything you do in a shell script will have sloppy timing,
>         maybe a few seconds off.  But you can do this for any process,
>         using the following crude approach within a single script:
>         
>         This script should be run with all its output redirected to a
>         log file, so you can have a record of how it went.
>         
>         Run your process (tcpdump) in the background with & 
>         
>         This becomes an independent process, so the next command in
>         your script will start immediately:
>         
>         date  # output goes into your log file.
>         
>         sleep 86399  # Or less, since kill won't be instantaneous
>         
>         date
>         
>         Use a pipeline with "ps -ef" and "grep" to identify the
>         running tcpdump process.  Extract the pid using "cut" and do a
>         "kill".
>         
>         sleep 2   # just to give kill time to take effect
>         
>         ps -ef | grep ... # Did it go away?
>         
>         date
>         
>         exit
>         
>         Details on request, but the above commands are good things to
>         learn.  This type of ps + grep pipeline is also useful to
>         detect if a duplicate copy of a script is running, etc.
>         
>         The sleep command is only precise to within a second or two,
>         and other system activity might delay the next command.
>         
>         Vic
>         
>         
>         --------------------------------------------------- 
>         PLUG-discuss mailing list -
>         PLUG-discuss at lists.plug.phoenix.az.us
>         To subscribe, unsubscribe, or to change  you mail settings:
>         http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
> 
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.plug.phoenix.az.us
> To subscribe, unsubscribe, or to change  you mail settings:
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
-- 
Bill Warner <wwarner42 at cox.net>



More information about the PLUG-discuss mailing list