Running a shell command for a specific period of time
Dan Lund
situationalawareness at gmail.com
Wed Oct 26 13:36:30 MST 2005
Well "time" essentially does what the other script in this thread
does, in a way. It just sits in the background until the process
dies, then compares the start and end time. (my guess, that's how I'd
do it)
On 10/26/05, Erik Bixby <erik.bixby at gmail.com> 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
>
>
--
To exercise power costs effort and demands courage. That is why so
many fail to assert rights to which they are perfectly entitled -
because a right is a kind of power but they are too lazy or too
cowardly to exercise it. The virtues which cloak these faults are
called patience and forbearance.
Friedrich Nietzsche
More information about the PLUG-discuss
mailing list