timeout is a command line utility that executes a specified command and stops it if it is still running after a certain period of time. In other words, the command
timeout lets you run the command with a timeout that you specify. The command
timeoutis part of the core GNU utility package that is installed on almost all Linux distributions.
This command is useful when you want to run a command that doesn’t have a built-in timeout option, or stop a process after a certain amount of time running.
In this article, we will explain how to use commands
timeout in Linux Terminal.
How to Use the Timeout Command
The syntax for the timeout command is as follows:
timeout [OPTIONS] DURATION COMMAND [ARG]…
DURATION can be a positive integer or floating-point number, followed by an optional unit suffix:
s– seconds (seconds) (this is the default option)
m– minutes (minutes)
h– hours (hours)
d– days (days)
When no units are used, the default is seconds. If the duration is set to zero, the associated timeout is disabled.
Command options must be provided before the argument.
Here are some basic examples showing how to use the command
- Stop the command
pingafter five seconds::timeout 5 ping 18.104.22.168
- Stop the command after five minutes:timeout 5m ping 22.214.171.124
- Stop the command after one minute and six seconds:timeout 1.1m ping 126.96.36.199
If you want to run a command that requires privileges like
tcpdump, add a prefix
sudo before the command
sudo timeout 300 tcpdump -n -w data.pcap
Sending Specific Signals
If no signal is given, it
timeout will use the SIGTERM signal to the specified command when the timeout is reached. You can specify which signal to use using the
--signal) option .
For example, to send
SIGKILLto a command
ping after one minute, you can use the command:
sudo timeout -s SIGKILL ping 188.8.131.52
Signal can be specified by its name like
SIGKILLor number like
9. The following command is identical to the previous one:
sudo timeout -s 9 ping 184.108.40.206
To get a list of all available signals, use the command
Kill the stuck process
SIGTERM, the default signal sent when the timeout is exceeded may be caught or ignored by some processes. In that situation, the process continues to run after the termination signal is sent.
To make sure the monitored command has stopped, use the option
--kill-after) followed by a time period. When this option is used after the given timeout is reached, the command
timeout sends a signal
SIGKILLto the program corresponding to the user’s input.
In the following example, it
timeout runs the command for one minute, and if not terminated, it will “kill” the program after ten seconds:
sudo timeout -k 10 1m ping 220.127.116.11
timeout -k “./test.sh”
The process will be killed after the given time limit is reached even though it is stuck.
Maintaining Exit Status
timeout will return
124when the time limit has been reached. Otherwise, the command
timeout will return the exit status of the managed command.
To return the exit status of the command even when the timeout is reached, use the option
timeout --preserve-status 5 ping 18.104.22.168
Running commands in Foreground
By default, it
timeout runs a command in the background. If you want to run the command in the Foreground, use the option
timeout --foreground 5m ./script.sh
This option is useful when you want to run interactive commands that require user input.
timeout used to run a certain command with a time limit according to the will of the user.
timeout are simple commands that don’t have many options. Usually you will use a command
timeout with only two arguments, duration, and the command that will occur.