Sometimes you need to stop a command, if it executes for too long. It’s very simple with a timeout utility from coreutils package. timeout is available in coreutils 8.X – recent enough version is currently in Debian squeeze and sid only. The version in Ubuntu 10.04 is too old (does not provide timeout). Usage is simple:


For example:

% timeout 2 sleep 100
% echo $?

The command should stop after 2 seconds and the return code will equal to 124 – this is what timeout returns if a command was timed out.
timeout will send TERM signal to a command which may be intercepted by a command and ignored (process will not stop). You can send KILL signal to make sure that a command will stop:

% timeout -s KILL 2 sleep 100