Timeout Command in Linux
timeout is a command-line utility that runs a specified command and terminates it whether it is nonetheless working after a given time period. In different phrases,
timeout lets you run a command with a time restrict. The
timeout command is part of the GNU core utilities package deal which is put in on virtually any Linux distribution.
It’s useful once you need to run a command that doesn’t have a built-in timeout choice.
On this article, we’ll clarify how one can use the Linux
How one can Use the
timeout Command #
The syntax for the
timeout command is as follows:
timeout [OPTIONS] DURATION COMMAND [ARG]…
DURATION is usually a constructive integer or a floating-point quantity, adopted by an non-obligatory unit suffix:
s– seconds (default)
When no unit is used, it defaults to seconds. If the length is ready to zero, the related timeout is disabled.
The command choices have to be supplied earlier than the arguments.
Listed here are a couple of fundamental examples demonstrating how one can use the
- Terminate a command after 5 seconds:
timeout 5 ping 126.96.36.199
- Terminate a command after 5 minutes:
timeout 5m ping 188.8.131.52
- Terminate a command after one minute and 6 seconds:
timeout 1.1m ping 184.108.40.206
sudo timeout 300 tcpdump -n -w data.pcap
Sending Particular Sign #
If no sign is given,
timeout sends the
SIGTERM sign to the managed command when the time restrict is reached. You may specify which sign to ship utilizing the
For instance, to ship
SIGKILL to the
ping command after one minute you’d use:
sudo timeout -s SIGKILL ping 220.127.116.11
The sign will be specified by its title like
SIGKILL or its quantity like
9. The next command is an identical to the earlier one:
sudo timeout -s 9 ping 18.104.22.168
To get an inventory of all out there alerts, use the
kill -l command:
Killing Caught Processes #
SIGTERM, the default sign that’s despatched when the time restrict is exceeded will be caught or ignored by some processes. In that conditions, the method continues to run after the termination sign is ship.
To verify the monitored command is killed, use the
--kill-after) choice following by a time interval. When this selection is used after the given time restrict is reached, the
timeout command sends
SIGKILL sign to the managed program that can’t be caught or ignored.
Within the following instance,
timeout runs the command for one minute, and if it’s not terminated, it’ll kill it after ten seconds:
sudo timeout -k 10 1m ping 22.214.171.124
timeout -k “./check.sh”
killed after the given time restrict is reached
Preserving the Exit Standing #
124 when the time restrict is reached. In any other case, it returns the exit standing of the managed command.
To return the exit standing of the command even when the time restrict is reached, use the
timeout --preserve-status 5 ping 126.96.36.199
Working in Foreground #
timeout runs the managed command within the background. If you wish to run the command within the foreground, use the
timeout --foreground 5m ./script.sh
This selection is helpful once you need to run an interactive command that requires person enter.
timeout command is used to run a given command with a time restrict.
timeout is an easy command that doesn’t have quite a lot of choices. Usually you’ll invoke
timeout solely with two arguments, the length, and the managed command.
When you’ve got any questions or suggestions, be happy to go away a remark.