Execute a command or pipe only for a given duration.
# $Header: /CVSROOT/timeout/DESCRIPTION,v 1.3 2010-10-20 23:12:30 tino Exp $
#
# $Log: DESCRIPTION,v $
# Revision 1.3 2010-10-20 23:12:30 tino
# fork problem returns 210, child exec fail returns 127 (like bash)
#
# Revision 1.2 2008-11-02 00:13:56 tino
# First version
#
Timeout is a complete rewrite of a program which I had before. It is
used to restrict the time duration a program runs. Note that the
"timeout" command which comes with Debian Etch is seriously broken,
therefor I had to create this one here.
Just call it as
timeout [-v] [-signal[@seconds]...] seconds [-|program [args...]]
- If -v is present, verbose information is printed.
- If -v is doubled, the current timeout is shown.
- Returns the exit status of the program, 127 if child exec fails, 210 on error from timeout (like usage).
- If execution takes longer than the given number of seconds,
timeout terminates the program and returns 255.
- If signals are given, the signals are sent to the program with
the given delay between. The default is -1 -15 -9 and a one second
delay.
- If program is missing it works like /bin/cat. In this case, it
just terminates after the given duration with exit status 211.
- If program is given as '-' the timeout occurs after a silence
phase of the given duration. Silence is either reading or writing
starves.
Future (not yet implemented) plans:
- Seconds can hava a fraction. This is no real floating point
number.
- On HUP or TERM, the timeout cycle for signals is started.
- If program is given as '-number' then the process is attached to
and the signal is sent to the process after this time if the process
still exists (is like a delayed kill). This is free from race
conditions like "sleep $timeout; kill -9 $pid".
- Allow '-[n]:program' to check stdin, stdout or stderr for silence
(pipe)
- Implement gerneric extended socket processing, this is, program
can be given as connect:host:port or connect:/unixsocket etc. If
prefixed with - it, again, monitors for silence. This shall work for
"exec:"- and "pty:"-URLs, too, which open a socket to a process.
Examples:
- timeout 5
same as 'sleep 5'
- timeout 5 < /var/log/messages
same as 'sleep 5 & tail -+0 --pid=$! -f /var/log/messages'
- timeout 5 - < /var/log/messages
nearly the same as
'cat /var/log/messages | while read -t5 data; do echo "$data"; done'
- timeout 5 ls
nearly same as
'ls & PID=$!; { sleep 5; kill $PID; } & PID2=$!; wait $PID; kill
$PID2'
(Please note that 'ls & PID=$!; sleep 5; kill $PID' would always delay
5 seconds.)