You can do it with a simple script like this:
#!/bin/bash
#
# $1 is the time to let the program run, and $2, $3, ... are the command itself.
#
"${@:2}" &
PID=$!
sleep "${@:1:1}"
kill -2 $PID
(signal SIGINT=2 used as per Matija Nalis' suggestion in the comment below).
An explanation of the (uncommon?) bash expression $@:...
: the positional parameters, ($*, $@, "$*", "$@"
) admit the following extra specification, for instance:
"${@:START:COUNT}"
which means: of all parameters, take COUNT of them, the first one to be taken being that at the STARTth position; if COUNT is omitted, take all of them till the end, beginning with the STARTth position. Remember that $0
is the program name. If START is negative, start counting from the end, and remember that COUNT cannot be negative, hence the last argument is "${@:-1}"
. Also, just about always include the positional parameters inside double quotes.
10Why would you use one rather than the other? – Brice M. Dempsey – 2016-02-25T13:42:21.733
2@BriceM.Dempsey On my local machine, timeout is present, but timelimit is not (although it is available from the repo). So, if one of them comes pre-installed... Apart from that, just looking at the method signatures I can see that they do different things, and have different use cases. – Benubird – 2016-02-25T15:49:24.553
Note that there exist at least two implementations of
– sch – 2016-02-25T16:07:53.753timelimit
. See How can I kill a process and be sure the PID hasn't been reused for more info on them and GNU timeout.1
@BriceM.Dempsey
– Hastur – 2016-02-25T16:24:02.127timeout
is included in GNU coreutils,timelimit
is not. You can have installed none, one or both. For the second it is reported timelimit executes a command and terminates the spawned process after a given time with a given signal. A “warning” signal is sent first, then, after a timeout, a “kill” signal, similar to the way init(8) operates on shutdown. So even a different in the middle behaviour.Since GNU coreutils is preinstalled on every GNU/Linux,
timeout
is the way to go there. – rexkogitans – 2016-02-26T09:17:29.353@rexkogitans, I've edited my answer to address your comments. – Toby Speight – 2016-02-26T09:49:46.297
can any of these programs kill the whole process tree (like,
kill -TERM -1234
kills PID 1234 and its children) – törzsmókus – 2018-02-07T11:56:17.800Not as far as I know, @törzsmókus - you'll need to modify one of the tools, or perhaps write a small wrapper program that catches signals and then re-signals its own process group. Of course, that has limitations, given that KILL and STOP are uncatchable. – Toby Speight – 2018-02-07T12:03:34.883
thx @TobySpeight! I wonder then how can
kill -TERM -1234
_just work™_… – törzsmókus – 2018-02-07T20:59:45.107@törzsmókus - no, that bit's fine; what you can't do is write a program that handles those signals - it will be stopped before it's able to then kill its process group. Try it in Bash:
trap 'kill -KILL -$$' KILL
for example. (Of course, doing the same thing with SIGTERM is possible - it's only STOP and KILL that are uncatchable). – Toby Speight – 2018-02-08T08:04:00.403