This answer to Command line command to auto-kill a command after a certain amount of time
proposes a 1-line method to timeout a long-running command from the bash command line:
( /path/to/slow command with options ) & sleep 5 ; kill $!
But it's possible that a given "long-running" command may finish earlier than the timeout. (Let's call it a "typically-long-running-but-sometimes-fast" command, or tlrbsf for fun.)
So this nifty 1-liner approach has a couple of problems. First, the sleep
isn't conditional, so that sets an undesirable lower bound on the time taken for the sequence to finish. Consider 30s or 2m or even 5m for the sleep, when the tlrbsf command finishes in 2 seconds — highly undesirable. Second, the kill
is unconditional, so this sequence will attempt to kill a non-running process and whine about it.
So...
Is there a way to timeout a typically-long-running-but-sometimes-fast ("tlrbsf") command that
- has a bash implementation (the other question already has Perl and C answers)
- will terminate at the earlier of the two: tlrbsf program termination, or timeout elapsed
- will not kill non-existing/non-running processes (or, optionally: will not complain about a bad kill)
- doesn't have to be a 1-liner
- can run under Cygwin or Linux
... and, for bonus points, runs the tlrbsf command in the foreground and any 'sleep' or extra process in the background, such that the stdin/stdout/stderr of the tlrbsf command can be redirected, same as if it had been run directly?
If so, please share your code. If not, please explain why.
I have spent awhile trying to hack the aforementioned example but I'm hitting the limit of my bash skills.
You are probably looking for the
timeout
command in coreutils. Since it's a part of coreutils, it is technically a C solution, but it's still coreutils.info timeout
for more details. Here's an example:There you go:
you may change the
SIGINT
and10
as you desire ;)My problem was maybe a bit different : I start a command via ssh on a remote machine and want to kill the shell and childs if the command hangs.
I now use the following :
This way the command returns 255 when there was a timeout or the returncode of the command in case of success
Please note that killing processes from a ssh session is handled different from an interactive shell. But you can also use the -t option to ssh to allocate a pseudo terminal, so it acts like an interactive shell
To timeout the
slowcommand
after 1 second:timeout 1 slowcommand || echo "I failed, perhaps due to time out"
You can do this entirely with
bash 4.3
and above:_timeout 5 longrunning_command args
{ _timeout 5 producer || echo KABOOM $?; } | consumer
producer | { _timeout 5 consumer1; consumer2; }
Example:
{ while date; do sleep .3; done; } | _timeout 5 cat | less
Needs Bash 4.3 for
wait -n
If you do not need the return code, this can be made even simpler:
Notes:
Strictly speaking you do not need the
;
in; )
, however it makes thing more consistent to the; }
-case. And theset +b
probably can be left away, too, but better safe than sorry.Except for
--forground
(probably) you can implement all variantstimeout
supports.--preserve-status
is a bit difficult, though. This is left as an exercise for the reader ;)This recipe can be used "naturally" in the shell (as natural as for
flock fd
):However, as explained above, you cannot re-export environment variables into the enclosing shell this way naturally.
Edit:
Real world example: Time out
__git_ps1
in case it takes too long (for things like slow SSHFS-Links):Edit2: Bugfix. I noticed that
exit 137
is not needed and makes_timeout
unreliable at the same time.Edit3:
git
is a die-hard, so it needs a double-trick to work satisfyingly.Edit4: Forgot a
_
in the first_timeout
for the real world GIT example.This solution works regardless of bash monitor mode. You can use the proper signal to terminate your_command
The watcher kills your_command after given timeout; the script waits for the slow task and terminates the watcher. Note that
wait
does not work with processes which are children of a different shell.Examples: