Sometimes you may need to set a timeout on a Linux command or program, and terminate it if the execution continues after timeout. You can easily do this using timeout command which is already installed by default on most Linux distributions. It allows you to set a timeout value on almost every script, command and program. It is especially useful for programs that do not have a built in timeout mechanism.
Timeout Command in Linux
Here is how to use timeout command in Linux. Here is the syntax for timeout command.
timeout [OPTIONS] DURATION COMMAND [ARG]…
The duration can be an integer or float, optionally followed by unit of time.
s– seconds (default)
If no unit is mentioned, seconds is considered. If you set duration as zero, then timeout is disabled. You should mention the command or script for timeout only after you have mentioned the options and duration for timeout command. Here are some examples to get started.
Here is the command to terminate execution after 5 seconds.
$ timeout 5 ping 18.104.22.168
Here is the command to terminate execution after 10 seconds.
$ timeout 10s ping 22.214.171.124
Here is the command to terminate execution after 2 minute 30 seconds.
$ timeout 2.5m ping 126.96.36.199
If you need sudo permission to run your command, you need to specify it before timeout command.
$ sudo timeout 300 tcpdump -n -w data.pcap
timeout command allows you to send specific signals after timeout period is over. By default, it sends SIGTERM command when time limit is reached. Instead, you can also send SIGKILL signal instead using -s option.
$ sudo timeout -s SIGKILL ping 188.8.131.52
Alternatively, you can also specify the signal number like 9.
$ sudo timeout -s 9 ping 184.108.40.206
The SIGTERM signal sent by timeout command may be ignored by certain processes. In such cases you can also instruct timeout command to send SIGKILL command after another interval of time, using -k option. Here is a command to send SIGTERM signal after 1 minute, and then send SIGKILL command after 10 seconds, in case the process it not killed.
$ sudo timeout -k 10 1m ping 220.127.116.11
Timeout command returns exist status 124 in case timeout is reached, else it returns the exit status of command. If you want to capture the exit status you can do so with –preserve-status option.
$ timeout --preserve-status 5 ping 18.104.22.168
Another thing to remember is that by default, timeout command runs the monitored command in background. If you want to run it foreground, then you need to use –foreground.
$ timeout --foreground 5m ./script.sh
Timeout is a very useful command that allows you to easily set timeouts and prevent processes from unnecessarily running for a long time and consuming system resources.
How to Set SSH Warning Message in Linux
2 thoughts on “Timeout Command in Linux”
There is a typo in:
“Here is the command to terminate execution after 10 seconds.
$ timeout 10m ping 22.214.171.124”
If you use the above command, the timeout will set to 10 minutes, not to 10 seconds…
Thank you so much for pointing it out. We have updated the post as per your feedback.