timeout It is a command line utility that executes and terminates the specified command if it is running after a certain period of time. In other words, orders
timeout It allows you to run orders within the time limit you specify. Command
timeout It is part of the basic GNU Toolkit installed on most Linux distributions.
This command is useful when you want to run a command that doesn’t have a built-in timeout option, or to stop a process after a certain time has passed.
In this article we will explain how to use commands
timeout In Linux Terminal.
How to use the timeout command
The syntax of the timeout command is as follows:
timeout [OPTIONS] DURATION COMMAND [ARG]…
DURATION It can be a positive integer or a decimal, followed by an optional unit suffix:
s– seconds (this is the default)
When no unit is in use, the default setting is seconds. If duration is set to zero, the associated timeout is disabled.
Command options must be provided before the arguments.
Here are some basic examples that show how to use the commands
- Stop orders
pingFive seconds later:
timeout 5 ping 22.214.171.124
- Stop it after five minutes:
timeout 5m ping 126.96.36.199
- Stop it after a minute and six seconds:
timeout 1.1m ping 188.8.131.52
If you want to run privileged commands like
tcpdump, First add the prefix
sudo Before ordering
sudo timeout 300 tcpdump -n -w data.pcap
Send certain signals
If no indication is given,
timeout Will use the SIGTERM signal to a specified command when the time limit is reached. You can select which signal you want to use using the options
For example, to send
SIGKILL To order
ping After a minute, you can use the command:
sudo timeout -s SIGKILL ping 184.108.40.206
The reference can be identified with a name like
SIGKILL Or numbers like
9. The following commands are the same as the previous ones:
sudo timeout -s 9 ping 220.127.116.11
To get a list of all available signals, use the command
Kill free operations
SIGTERM, The default signal sent when the timeout is exceeded or ignored by some process can be captured. In this case, the process continues after the exit signal has been sent.
To make sure that the watched command stops, use the option
--kill-after) Followed by a period of time. When this option is used after the specified time limit has been reached, commands are issued
timeout Signal transmission
SIGKILL To the program that matches the user’s input.
In the following example,
timeout It runs the command for one minute, and if not stopped, it will “kill” the program after ten seconds:
sudo timeout -k 10 1m ping 18.104.22.168
timeout -k “./test.sh”
Even if it crashes, it will kill the process after reaching the specified timeout.
Maintain exit status
timeout will return
124 When the time limit has been reached. If not, ask
timeout It will restore the exit status of the managed order.
To return the exit status of the command even when the timeout has been reached, use the option
timeout --preserve-status 5 ping 22.214.171.124
Run the commands in the front
timeout Run a command in the background. If you want to run commands in the foreground (foreground), then use the options
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 specific command with a time limit according to the user’s will.
timeout It’s simple and doesn’t have many options. Usually you will use the command
timeout With only two arguments, duration (duration), and one command that will happen.