Running a shell command for a specific period of time

Lynn David Newton lnewton at mvista.com
Wed Oct 26 09:49:27 MST 2005


  e> I was wondering if anyone knew a way to allow a
  e> shell command to run for a specific period of
  e> time. ...

Lots of solutions. One simple one would be to put
something like this in a script and run it.

----------------------- doit.sh -----------------------
#!/bin/bash
nohup somecmd arguments &
pid=$!
sleep 86399
kill -9 $pid
------------------------- end -------------------------

Of course "somecmd arguments" is whatever it is you
want to run for the specified period of time.

There is no guarantee that the command will expire on
its own before the sleep timeout.

Technically the nohup is not needed, but it will save
output to nohup.out without having to specify it. 
Saving $! as pid is also technically superfluous, but
not a bad idea in practice, because $! can change in
the course of a longer script.

-- 
Lynn David Newton
MontaVista Software, Inc.
2141 E. Broadway Road, Suite 108
Tempe, AZ 85282
Phone: 480-517-5047
Email: lnewton at mvista.com


More information about the PLUG-discuss mailing list