How to use the timeout command in Linux

Contents

Linux laptop showing a bash prompt

Agree, enough time on the computer. You can give processes time limits, establishing a maximum time that can be executed with the timeoutcommand. Here is a tutorial to put limits on the execution of programs with this command.

What does the waiting time do for you?

the timeout the command enables you set a limit on the amount of time a program will run. But, Why would you want to do that?

A case is when you know exactly how long you want a procedure to run. A common use case is to have timeout control a data capture or logging program so that log files don't relentlessly consume your hard drive space.

Another case is when you don't know how long you want a procedure to run, but you do know that you don't want it to run indefinitely. You may be in the habit of configuring running processes, minimize terminal window and forget about them.

Some programs, even simple utilities, can generate network traffic at levels that can impede the performance of your network. Or they can freeze resources on a target device, which slows down your performance. (ping, I'm looking at you.) Leaving these types of programs running for long periods of time while you are not at your computer is bad practice..

timeout is part of GNU Core Utilities because, Linux and Unix-like operating systems, like macOS, have built-in waiting time. There is nothing to install; you can use it right out of the box.

Starting with the waiting time

Here is a simple example. As an example, with your default command line options, the ping The command will run until you stop it by pressing Ctrl + C. If you don't interrupt, to be continue.

ping 192.168.4.28

Through use timeout, we can make sure ping does not run intermittently, chewing on network bandwidth and pestering any device that gets pinged.

This next command uses timeout to the time limit ping. We allow 15 runtime seconds for ping.

timeout 15 ping 192.168.4.28

After 15 seconds timeout ends the ping session and return to the command line.

Using the timeout with other time units

Note that we did not have to add a “s” behind 15. timeout assumes the value is in seconds. Could add a “s”, but it really makes no difference.

To use a time value measured in minutes, hours or days, add a “m”, a “h” or one “d”.

For the ping to run for three minutes, use the following command:

timeout 3m ping 192.168.4.28

ping will work for three minutes before timeout intervenes and stops the ping session.

ping session running on a widowed terminal

Limit data capture with timeout

Some data capture files can grow very quickly. To prevent such files from becoming unwieldy or even problematic in size, limit the amount of time the capture program is allowed to run.

In this example, we are using tcpdump, a network traffic capture tool. On the test machines this post was investigated on, tcpdump it was already installed on Ubuntu Linux and Fedora Linux. It had to be installed on Manjaro Linux and Arch Linux, with the following command:

sudo pacman -Syu tcpdump

We can run tcpdump during 10 seconds with your default options and redirect your output to a file called capture.txt with the following command:

timeout 10 sudo tcpdump > capture.txt

(tcpdump has its own options for saving captured network traffic to a file. This is a quick hack because we are discussing timeout, no tcpdump.)

tcpdump starts capturing network traffic and we wait 10 seconds. AND 10 seconds come and go and tcpdump is still running and capture.txt is still growing in size. A Ctrl will be needed + C hasty to stop tcpdump.

Checking the size of capture.txt with ls shows it grew to 209K in seconds. That file was growing rapidly!

ls -lh capture.txt

What happened? Why not timeout stop tcpdump?

It all has to do with the signs.

Send the correct signal

When timeout wants to stop a program, sends the Signal SIGTERM. This politely asks the show to end. Some programs may choose to ignore the SIGTERM signal.. When that happens, we have to say timeout to be a little more forceful.

We can do that by asking timeout to send the SIGKILL signal instead.

The SIGKILL signal cannot be “captured, blocked or ignored", always happens. SIGKILL does not politely ask the program to stop. SIGKILL hides around the corner with a stopwatch and a cosh.

We can use the -s (sign) option to count timeout to send the SIGKILL signal.

timeout -s SIGKILL 10 sudo tcpdump > capture.txt

This time, as soon as they have elapsed 10 seconds, tcpdump is stopped.

Ask politely first

We can ask timeout to try to stop the program using SIGTERM, and send only SIGKILL if SIGTERM didn't work.

To do this, we use the -k (kill later) option. the -k The option needs a time value as a parameter.

In this command, we are asking timeout let dmesg run for 30 seconds and then terminate it with the SIGTERM signal. And dmesg still works after 40 seconds, means that the diplomatic SIGTERM was ignored and timeout you must send SIGKILL to finish the job.

dmesg is a utility that can monitor kernel ring buffer messages and display them in a terminal window.

timeout -k 40 30 dmseg -w

dmesg works for 30 seconds and stops when it receives the SIGTERM signal.

We know it wasn't SIGKILL who stopped dmesg because SIGKILL always leaves a one word obituary in the terminal window: “Murdered”. That did not happen for this case.

Retrieve the program exit code

Well-behaved programs return a value to the shell when they finish. This is known as an exit code.. In general, this is used to tell the shell, or any procedure that the program has started, if the program encountered problems while running.

timeout provide your own exit code, but it is possible that that does not matter to us. We are probably more interested in the exit code of the procedure than timeout is controlling.

This command enables ping run for five seconds. You are pinging a computer called Nostromo, which is on the testnet that was used to research this post.

timeout 5 ping Nostromo.local

The command runs for five seconds and timeout finish it. Then we can check the exit code using this command:

echo $?

The exit code is 124. This is the value timeout used to indicate that the program was terminated using SIGTERM. If SIGKILL terminates the program, the exit code is 137.

If we interrupt the program with Ctrl + C the exit code of timeout is zero.

timeout 5 ping Nostromo.local
echo $?

If program execution ends before timeout finish it, timeout you can pass the program exit code to the shell.

For this to happen, the program should stop on its own (In other words, it is no finished by timeout), and we must use the --preserve-status option.

If we use the -c (count) option with a value of five ping will only trigger five requests. If we give timeout a one minute extension, ping it will definitely be over by itself. Then we can check the output value using echo.

timeout --preserve-status 1m ping -c 5 Nostromo.local
echo $?

ping completes its five ping requests and terminates. The exit code is zero.

To verify that the exit code comes from ping, let's force ping to generate a different exit code. If we try to send ping requests to a non-existent IP address, ping will fail with an error exit code. Then we can use echo to check that the exit code is different from zero.

timeout --preserve-status 1m ping -c 5 NotHere.local
echo $?

the ping evidently, command cannot reach non-existent device, so it reports the error and closes. The exit code is two. This is the exit code ping uses for general errors.

Determine ground rules

timeout it's about providing some limits to running programs. If there is a danger of log files invading your hard drive or forgetting that you left a network tool running, wrap them in timeout and let your computer regulate itself.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.