OK, that's enough computer time. You can use the
timeout command to set process timeouts and set a maximum runtime. In this tutorial, you will learn how to limit the execution of programs with this command.
What does timeout mean to you?
You can use the
Timeout command to limit the runtime of a program. But why should you do that?
A case is when you know exactly how long a process should run. A common use case is
timeout to control a logger or data collector so that the log files do not inexorably devour your hard disk space.
Another case is when you do not know how long a process should run, but you know that it should not run indefinitely. You may have the habit of activating processes, minimizing and forgetting the terminal window.
Some programs-even simple utilities-can generate network traffic at a level that can affect the performance of your network. Or they tie up resources on a target device, slowing down its performance. (
ping I'm looking at you.) It's a bad practice to run this type of program for a long time while you are away from your computer.
Timeout is part of the GNU Core Utils, Linux and Unix-like operating systems like macOS all have a built-in timeout. There is nothing to install; You can use it immediately.
Getting Started with Timeout
Here's a simple example. For example, the default command-line options execute
ping until you exit it by pressing Ctrl + C. If you do not interrupt, it just keeps going.
timeout we can ensure that
ping is not constantly executed which chews the network bandwidth and hardens the device being pinged.
In this next command,
timeout is used to temporally limit
ping . We allow 15 seconds for
Timeout 15 Ping 192.168.4.28
After 15 seconds
Timeout ends the ping session
and we return to the command prompt back.
Using timeout with other time units
Note that we have no "s" behind the 15th
time limit had to insert if the value is in seconds. You can add an "s", but it really does not make a difference.
To use a time value measured in minutes, hours or days, add an "m", an "h" or a "d".
Use the following command to ping for three minutes:
Timeout 3m Ping 192.168.4.28
Ping is executed for three minutes before the
timeout occurs and stops the
ping session .
Restricting Data Collection with Timeout  Some data collection files can grow very fast. To prevent such files from becoming unwieldy or even problematic, you should limit the execution time of the collection program.
In this example, we use
tcpdump a network traffic collection tool. On the test machines that researched this article,
tcpdump was already installed in Ubuntu Linux and Fedora Linux. It had to be installed under Manjaro Linux and Arch Linux with the following command:
sudo pacman -Syu tcpdump
We can execute
tcpdump for 10 seconds with the default options and redirect the output to a file called capture.txt with the following command:
timeout 10 sudo tcpdump> capture.txt
tcpdump starts capturing network traffic and we wait 10 seconds and 10 seconds come and go and
tcpdump becomes still running, u nd capture.txt is getting bigger. It will take a premature press of Ctrl + C to pause.
Checking the size of capture.txt with
ls shows that it grew to 209 KB in seconds. This file grew fast!
ls -lh capture.txt
What happened? Why did
the timeout not stop
It all depends on signals.
Sending the Right Signal
is timed out stop a program that sends the SIGTERM signal. This politely asks for the program to end. Some programs may ignore the SIGTERM signal. If this happens, we need to instruct
Timeout to be a little more energetic.
We can do this by asking
Timeout to send the SIGKILL signal instead.
The SIGKILL The signal can not be "intercepted, blocked or ignored" - it always comes through. SIGKILL does not politely ask the program for a stop. SIGKILL hides around the corner with a stopwatch and a cosh.
-s (Signal) option to specify
Timeout to send the SIGKILL signal.
Timeout -s SIGKILL 10 sudo tcpdump> capture.txt
First politely asking you to stop the program with SIGTERM and only then Send SIGKILL if SIGTERM did not work.
We use the option
-k (kill after) The option
- Option k requires a time value as parameter. [19659006Inthiscommandwerequest
timeout to run
dmesg for 30 seconds and then terminate signaling with the SIGTERM If
dmesg is still running after 40 seconds This means that the diplomatic SIGTERM is ignorant
should send the time-out SIGKILL to complete the order.
dmesg is a utility that can monitor kernel ring buffer messages and display them in a terminal window.
Time limit -k 40 30 dmseg -w
dmesg Runs for 30 seconds and stops when the SIGTERM Signal is received.
We know SIGKILL has not stopped
dmesg because SIGKILL always leaves an obituary in the terminal window in one word: "Killed". This did not happen in this case.
Retrieving the Program Exit Code
Well-behaved programs pass a value back into the shell when they end. This is called an exit code. This is typically used to tell the shell or the process that started the program if there were problems running the program.
timeout provides its own exit code, but we may not be interested in that. We're probably more interested in the exit code from the process that controls
This command allows
ping to run for five seconds. It pings a computer called Nostromo, which is on the test network that researched this article.
Timeout 5 pings Nostromo.local
The command runs for five seconds, and ends
timeout . With this command we can then check the exit code:
The exit code is 124. This is the value
with the timeout indicated that the program was terminated with SIGTERM. When SIGKILL exits the program, the exit code is 137.
If we break the program with Ctrl + C, the exit code of
Timeout is zero.
Timeout 5 ping Nostromo.local
If execution of the program ends before
timeout may exit Return code from the program to the shell.
To do this, the program must come to a standstill itself (ie does not finish ) until
timeout ), and we must retain the option
- Status use.
If we use the option
-c (count) with a value of five
ping will fire only five queries. If we limit
timeout to one minute,
ping has definitely stopped by itself. We can then check the output value with
timeout --preserve -status 1m ping -c 5 Nostromo.local
ping executes its five ping requests and quits. The exit code is 0.
To verify that the exit code is from
ping let's force
ping to generate another exit code. When we try to send ping requests to a non-existent IP address,
ping fails with an error exit code. We can then use
echo to see if the exit code is nonzero.
timeout --status retained 1m ping -c 5 NotHere.local
ping obviously can not reach the missing device and reports the error and shuts down , The exit code is two. This is the exit code that
ping uses for general errors.
Set Basic Rules
timeout is used to set some limits on executing programs. If there is a risk that the log files will overflow your hard drive, or you forget that you ran a network tool, wrap them in
Timeout and let your computer self-regulate.