A tutorial on using the timeout command in Linux

Command timeout It is a command line utility that executes and terminates the specified command if it is running after a certain period of time. In other words, orders timeout It allows you to run orders within the time limit you specify. Command timeout It is part of the basic GNU Toolkit installed on most Linux distributions.

This command is useful when you want to run a command that doesn’t have a built-in timeout option, or to stop a process after a certain time has passed.

In this article we will explain how to use commands timeout In Linux Terminal.

How to use the timeout command

The syntax of the timeout command is as follows:

timeout [OPTIONS] DURATION COMMAND [ARG]…

DURATION It can be a positive integer or a decimal, followed by an optional unit suffix:

  • s – seconds (this is the default)
  • m Minutes (minutes)
  • h Hours (hours)
  • d Days (days)

When no unit is in use, the default setting is seconds. If duration is set to zero, the associated timeout is disabled.

Command options must be provided before the arguments.

Here are some basic examples that show how to use the commands timeout :

  • Stop orders ping Five seconds later:
    timeout 5 ping 8.8.8.8
  • Stop it after five minutes:
    timeout 5m ping 8.8.8.8
  • Stop it after a minute and six seconds:
    timeout 1.1m ping 8.8.8.8

If you want to run privileged commands like tcpdump, First add the prefix sudo Before ordering timeout:

sudo timeout 300 tcpdump -n -w data.pcap

Send certain signals

If no indication is given, timeout Will use the SIGTERM signal to a specified command when the time limit is reached. You can select which signal you want to use using the options-s (--signal).

For example, to send SIGKILL To order ping After a minute, you can use the command:

sudo timeout -s SIGKILL ping 8.8.8.8

The reference can be identified with a name like SIGKILL Or numbers like 9. The following commands are the same as the previous ones:

sudo timeout -s 9 ping 8.8.8.8

To get a list of all available signals, use the command kill -l:

kill -l

Kill free operations

SIGTERM, The default signal sent when the timeout is exceeded or ignored by some process can be captured. In this case, the process continues after the exit signal has been sent.

To make sure that the watched command stops, use the option -k (--kill-after) Followed by a period of time. When this option is used after the specified time limit has been reached, commands are issued timeout Signal transmission SIGKILL To the program that matches the user’s input.

In the following example, timeout It runs the command for one minute, and if not stopped, it will “kill” the program after ten seconds:

sudo timeout -k 10 1m ping 8.8.8.8
timeout -k “./test.sh”

Even if it crashes, it will kill the process after reaching the specified timeout.

Maintain exit status

timeout will return 124 When the time limit has been reached. If not, ask timeout It will restore the exit status of the managed order.

To return the exit status of the command even when the timeout has been reached, use the option --preserve-status:

timeout --preserve-status 5 ping 8.8.8.8

Run the commands in the front

By default, timeout Run a command in the background. If you want to run commands in the foreground (foreground), then use the options --foreground:

timeout --foreground 5m ./script.sh

This option is useful when you want to run interactive commands that require user input.

Conclusion

Command timeout Used to run a specific command with a time limit according to the user’s will.

Command timeout It’s simple and doesn’t have many options. Usually you will use the command timeout With only two arguments, duration (duration), and one command that will happen.

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *