I'd like to automatically kill a command after a certain amount of time. I have in mind an interface like this:
% constrain 300 ./foo args
Which would run "./foo" with "args" but automatically kill it if it's still running after 5 minutes.
It might be useful to generalize the idea to other constraints, such as autokilling a process if it uses too much memory.
Are there any existing tools that do that, or has anyone written such a thing?
ADDED: Jonathan's solution is precisely what I had in mind and it works like a charm on linux, but I can't get it to work on Mac OSX. I got rid of the SIGRTMIN which lets it compile fine, but the signal just doesn't get sent to the child process. Anyone know how to make this work on Mac?
[Added: Note that an update is available from Jonathan that works on Mac and elsewhere.]
There is also ulimit, which can be used to limit the execution time available to sub-processes.
Limits the process to 10 seconds of CPU time.
To actually use it to limit a new process, rather than the current process, you may wish to use a wrapper script:
other-command can be any tool. I was running a Java, Python, C and Scheme versions of different sorting algorithms, and logging how long they took, whilst limiting execution time to 30 seconds. A Cocoa-Python application generated the various command lines - including the arguments - and collated the times into a CSV file, but it was really just fluff on top of the command provided above.
GNU Coreutils includes the timeout command, installed by default on many systems.
https://www.gnu.org/software/coreutils/manual/html_node/timeout-invocation.html
To watch
free -m
for one minute, then kill it by sending a TERM signal:The watcher kills the slow task after given timeout; the script waits for the slow task and terminates the watcher.
Examples:
Maybe I'm not understanding the question, but this sounds doable directly, at least in bash:
This runs the first command, inside the parenthesis, for five seconds, and then kills it. The entire operation runs synchronously, i.e. you won't be able to use your shell while it is busy waiting for the slow command. If that is not what you wanted, it should be possible to add another &.
The
$!
variable is a Bash builtin that contains the process ID of the most recently started subshell. It is important to not have the & inside the parenthesis, doing it that way loses the process ID.pure bash:
I have a program called
timeout
that does that - written in C, originally in 1989 but updated periodically since then.Update: this code fails to compile on MacOS X because SIGRTMIN is not defined, and fails to timeout when run on MacOS X because the
signal()
function there resumes thewait()
after the alarm times out - which is not the required behaviour. I have a new version oftimeout.c
which deals with both these problems (usingsigaction()
instead ofsignal()
). As before, contact me for a 10K gzipped tar file with the source code and a manual page (see my profile).If you want the 'official' code for 'stderr.h' and 'stderr.c', contact me (see my profile).