bash-timeout is a command and also a bash function in order to terminate the target command if the target command does not finish within the duration specified beforehand. The input via either redirection ( < FILE ) or pipe ( | ) are transferred to the target command transparently. The exit status of the target command is retained if the target command finishes within the duration.
The timeout in GNU coreutils is a similar command which has the timeout capability. In shell script, bash-timeout make the script simpler than GNU timeout. See comparison.md for more details.
-
via pip
$ pip install bash-timeout
-
Use as a linux/unix command
$ bash-timeout 10s sleep 20s
-
Use as a bash function
source bash-timeout timeout 10s sleep 20s
-
An example in bash script
source bash-timeout function very_expensive_task() { ... } if timeout 10s very_expensive_task ; then echo "successfully done" else echo "the task failed or timed out" fi
-
Status code
-
Return error code if time out
$ bash-timeout 10s sleep 20s $ echo $? #=> 1 or more
-
Retain the target command status code if the target command finishes within the duration
-
Normal exit
$ bash-timeout 10s ls /bin $ echo $? #=> 0
-
Exit with error
$ ls /FOOBAR $ echo $? #=> 2
$ bash-timeout 10s ls /FOOBAR $ echo $? #=> 2
-
-
-
Input and Output
-
Retain output
$ echo abc #=> abc
$ bash-timeout 10s echo abc #=> abc
-
Retain input via pipe
$ echo abc | cat #=> abc
$ echo abc | bash-timeout 10s cat #=> abc
-
Retain input via redirection
$ echo abc > abc.txt $ cat < abc.txt #=> abc $ < abc.txt cat #=> abc
$ bash-timeout 10s cat < abc.txt #=> abc $ bash-timeout < abc.txt 10s cat #=> abc $ < abc.txt bash-timeout 10s cat #=> abc
-
- Takahide Nogayama - Nogayama
This project is licensed under the MIT License - see the LICENSE.txt file for details
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We express our sincere thanks to Scott Trent for reviewing documents.