[CentOS] Re: command to ensure other command does last longer than5 seconds

Sat Apr 7 16:00:14 UTC 2007
William L. Maltby <CentOS4Bill at triad.rr.com>

On Sat, 2007-04-07 at 12:47 -0300, Rodrigo Barbosa wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On Sat, Apr 07, 2007 at 10:30:10AM +0200, Jan-Frode Myklebust wrote:
> > On 2007-04-07, Shawn Everett <shawn at tandac.com> wrote:
> > > Just building off Micheal's idea:
> > >
> > > killafter.sh <command> <time>
> > > #!/bin/bash
> > >
> > > $1 &
> > > pid=$!
> > > sleep $2
> > > kill -TERM $pid
> > 
> > Just in case it might have died an recycled the pid, refer
> > to the job (%1), not the pid:
> > 
> > killafter.sh <command> <time>
> > #!bin/bash
> > $1 &
> > sleep $2
> > kill -TERM %1
> > 
> > 
> > Another way of doing it might be to fork a sub-shell with
> > limits:
> > 
> > 	(ulimit -t 1 ; top)
> > 
> > But this is cputime, not walltime...
> 
> You guys are forgetting 2 things:
> 
> 1) Maybe he doesn't want to run the process on the background.
> 
> 	This is somewhat easy to solve, with a little C program.
> You trap SIG_ALARM, fork, execp, and set and alarm. Another
> option would be to use wait/wait3/wait4 and alarm, which might
> be simpler. Or a combination of both.
> 
> 	but the main issue is
> 
> 2) You are forgeting to take starting up time in consideration
> 
> 	Say a problem will take 2 seconds to start up, due to
> any reason (disk I/O, memory, swap etc). If you specify 4 seconds
> as the limit, the problem will actually run for only 2 seconds.
> 
> 	I have no idea how to solve this second issue.
> 

Run the pre-defined script with a nice command (maybe -20, etc.), start
the process in background (with a somewhat smaller nice?), capture it's
start time and the current time (both in seconds since epoch), take
difference between them and subtract that from desired run duration,
round to seconds/minutes, ..., use that value.

> <snip sig stuff>

HTH
--
Bill