timeout command in linux

Timeout Command in Linux

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.


The duration can be an integer or float, optionally followed by unit of time.

  • s – seconds (default)
  • m – minutes
  • h – hours
  • d – days

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

Here is the command to terminate execution after 10 seconds.

$ timeout 10s ping

Here is the command to terminate execution after 2 minute 30 seconds.

$ timeout 2.5m ping

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

Alternatively, you can also specify the signal number like 9.

$ sudo timeout -s 9 ping

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

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

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.

Also read:

How to Set SSH Warning Message in Linux
How to Remove Specific Item from Array in JavaScript
Detect Mobile Device in JavaScript
How to Detect Click Outside Element in JavaScript
How to Capitalize First Letter in JavaScript

2 thoughts on “Timeout Command in Linux

  1. Hi,
    There is a typo in:
    “Here is the command to terminate execution after 10 seconds.
    $ timeout 10m ping”

    If you use the above command, the timeout will set to 10 minutes, not to 10 seconds…

Leave a Reply

Your email address will not be published. Required fields are marked *