How to use the timeout command on Linux

OK, that’s enough computer time. You can give processes time limits and a maximum runtime for which they can run with the timeout Command. Here is a tutorial on how to restrict programs from running with this command.

What does timeout mean to you?

the timeout Command enables you limit the length of time a program is running for. But why do you want to do that?

One case is when you know exactly how long a process should run. A common use case is to be had timeout Control a logging or data collection program so that the log files don’t inexorably gobble up your hard drive space.

Another case is when you don’t know how long a process should run but you know it shouldn’t run indefinitely. You may have a habit of running processes, minimizing the terminal window and forgetting about them.

Some programs – even simple utilities – can generate network traffic at levels that can affect the performance of your network. Or they tie up the resources on a target device and slow down its performance. ( ping (I’m looking at you.) Running these types of programs for long periods of time while away from your computer is bad practice.

timeout is part of GNU Core Utils Linux and Unix-like operating systems such as macOS have all timeouts integrated directly. There is nothing to install; You can use it right out of the box.

Getting started with timeout

Here is a simple one example. To the example, with its default command line options, the ping The command will run until you stop it by pressing Ctrl + C. If you don’t interrupt it, it just keeps going.


By using timeout , we can take care of it ping Doesn’t keep running, eats up network bandwidth and annoys any device that is pinged.

This next command used timeout time limit ping . We grant 15 seconds runtime for ping .

                      timeout 15 ping

After 15 seconds timeout ends the ping Session and we return to the command line prompt.

Use timeout with other time units

Notice that we didn’t have to add an “s” after the 15. timeout assumes that the value is given in seconds. You could add an “s” but it really doesn’t make any difference.

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

To let the ping run for three minutes, use the following command:

                      timeout 3m ping

ping runs three minutes before timeout enters and stops the ping Meeting.

Ping session running in a terminal widow

Restrict data collection with timeout

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

In this example, we use tcpdump , a Capture network traffic Tool. On the test machines on which this article was researched, tcpdump was already installed in Ubuntu Linux and Fedora Linux. It had to be installed on Manjaro Linux and Arch Linux using the following command:

                      sudo pacman -Syu tcpdump

We can walk 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 has its own options for saving the captured network traffic to a file. This is a quick hack because we’re debating timeout , not tcpdump .)

tcpdump starts collecting 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. It’ll take a hasty Ctrl + C to stop tcpdump .

Check the size of capture.txt with ls shows it grew to 209K in seconds. That file grew fast!

                      ls -lh capture.txt

What happened? Why not? timeout stop tcpdump ?

It has everything to do with signals.

Send the right Signal

When timeout want to stop a program sending it SIGTERM-Signal . This politely prompts the program to exit. Some programs can use the SIGTERMSignal to ignore. When that happens we have to say it timeout be a little more energetic.

We can do that by asking timeout to use the SIGKILL-Signal to send.

The SIGKILL-Signal cannot be “caught, blocked or ignored” – it always comes through. SIGKILL is not politely asking the program to stop. SIGKILL is hiding around the corner with a stopwatch and Cosh.

We can use that -s (Signal-) option to tell timeout around the SIGKILL-Signal to send.

                      timeout -s SIGKILL 10 sudo tcpdump > capture.txt

This time as soon as 10 seconds have passed tcpdump is stopped.

First ask politely

We can ask timeout trying to stop the program with SIGTERM and only send SIGKILL in if SIGTERM did not work.

We use the -k (kill after) option. the -k Option requires a time value as a parameter.

In this order we ask timeout permit dmesg Run for 30 seconds and then use the SIGTERMSignal break up. if dmesg after 40 seconds it is still running, it means that the diplomatic SIGTERM has been ignored and timeout should send SIGKILL in to finish the job.

dmesg is a utility that Monitor the kernel ring buffer messages and view them in a terminal window.

                      timeout -k 40 30 dmseg -w

dmesg runs for 30 seconds and stops when the SIGTERMSignal receives.

We know SIGKILL didn’t stop dmesg because SIGKILL always leaves a one-word obituary in the terminal window: “Killed”. That didn’t happen in this case.

Calling up the program Exit code

Well-behaved programs return a value to the shell when they exit. This is called ExitCode. Typically this is used to tell the shell – or the other process that started the program – whether the program encountered any problems while it was running.

timeout offers its own ExitCode, but we may not care. We’re probably more interested in that ExitCode of the process the timeout is controlled.

This command leaves ping run for five seconds. It pings a computer called Nostromo that is on the test network that was used to research this article.

                      timeout 5 ping Nostromo.local

The command runs for five seconds and timeout finish it. We can then ExitCheck the code with this command:

                      echo $?

Of the ExitCode is 124. This is the value timeout used to indicate that the program terminated with SIGTERM. When SIGKILL exits, the exit code is 137.

If we interrupt the program with Ctrl + C, the ExitCode from timeout is zero.

                      timeout 5 ping Nostromo.local
                      echo $?

When running the program before timeout finish it, timeout can do the Exit-Code of the program passed back to the shell.

To do this, the program must come to a standstill by itself (ie it is not terminated by timeout ), and we have to --preserve-status Opportunity.

If we use that -c (count) option with a value of five ping will only fire five requests. If we give timeout a duration of one minute, ping will definitely have ended on its own. We can then Exit-Check value with echo .

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

ping completes its five pings and exits. Of the ExitCode is null.

To see if the ExitCode is coming from ping , let’s force it ping to another Exit- generate code. When we try to ping a non-existent IP address, ping is displayed with an errorExitCode fail. We can then use echo to check if the ExitCode is not zero.

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

the ping Command obviously cannot reach the non-existent device, reports the error and closes. Of the ExitCode is two. That’s the Exit-Code ping used for general errors.

Establish basic rules

timeout it is about setting limits to the execution of programs. If the log files are at risk of spilling over your hard drive, or if you forget you were running a network tool, take them with you timeout and let your computer regulate itself.

Linux commands
Files tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processes alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networking netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts