قالب وردپرس درنا توس
Home / Tips and Tricks / Using the timeout command on Linux

Using the timeout command on Linux



  Linux Laptop with Bash Prompt
Fatmawati Achmad Zaenuri / Shutterstock.com

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.

  Ping 1
92.168.4.28

  Ping 192.168.4.28 in a Terminal Window

By using 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 Ping .

  Timeout 15 Ping 192.168.4.28 

  Timeout 15 Ping 192.168.4.28 in a Terminal Window

After 15 seconds Timeout ends the ping session and we return to the command prompt back.

 ends the ping session in a terminal window

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 

  Timeout 3m Ping 192.168.4.28 in a terminal window

Ping is executed for three minutes before the timeout occurs and stops the ping session .

 Ping Session in a Terminal Widow

Restricting Data Collection with Timeout [19659005] 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 

  sudo pacman -Syu tcpdump in a terminal window

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 

  timeout 10 sudo tcpdump> capture.txt in a terminal window " width = "646" height = "77" /> </p>
<p>  (<code> tcpdump </code> has its own options for saving captured network traffic to a file.) This is a quick hack, as we are discussing <code> timeout </code> not <code> tcpdump </code>.) </p><div><script async src=

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. tcpdump .

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 

  ls -lh capture.txt in a terminal window

What happened? Why did the timeout not stop tcpdump ?

It all depends on signals.

Sending the Right Signal

When 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.

Use the -s (Signal) option to specify Timeout to send the SIGKILL signal.

  Timeout -s SIGKILL 10 sudo tcpdump> capture.txt 

  timed -s sigkill 10 sudo tcpdump> capture.txt in a terminal window "width =" 646 "height =" 167 "/> </p>
<p>  This time, as soon as 10 seconds have passed, <code> tcpdump </code> is stopped. </p>
<p>  <img class=

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 

  Time limit -k 40 30 dmseg -w in a terminal window

dmesg Runs for 30 seconds and stops when the SIGTERM Signal is received.

 Issue of dmesg in a terminal window

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 timeout .

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 

  Timeout 5 pings Nostromo.local in a terminal window

The command runs for five seconds, and ends timeout . With this command we can then check the exit code:

  echo $? 

  Output from ping and echo $? in a terminal window

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 
  echo $? 

  timeout 5 ping Nostromo.local in a terminal window with Ctrl + C

If execution of the program ends before timeout 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 echo .

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

  timeout --preserve-status 1m ping -c 5 Nostromo.local in a terminal window

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 
  echo $? 

  timeout - -preserve-status 1m ping -c 5 NotHere.local in a terminal window

The command 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.




Source link