Tutorial on Using the Timeout Command on Linux

Tutorial on Using the Timeout Command on Linux

A command timeout is a command line utility that executes a specified command and stops it if it is still running after a certain period of time. In other words, the command  timeout  lets you run the command with a timeout that you specify. The command timeoutis part of the core GNU utility package that is installed on almost all Linux distributions.

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

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

How to Use the Timeout Command

The syntax for the timeout command is as follows:

timeout [OPTIONS] DURATION COMMAND [ARG]…

DURATION can be a positive integer or floating-point number, followed by an optional unit suffix:

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

When no units are used, the default is seconds. If the duration is set to zero, the associated timeout is disabled.

Command options must be provided before the argument.

Here are some basic examples showing how to use the command timeout :

  • Stop the command pingafter five seconds::timeout 5 ping 8.8.8.8
  • Stop the command after five minutes:timeout 5m ping 8.8.8.8
  • Stop the command after one minute and six seconds:timeout 1.1m ping 8.8.8.8

If you want to run a command that requires privileges like tcpdump, add a prefix sudo before the command timeout:

sudo timeout 300 tcpdump -n -w data.pcap

Sending Specific Signals

If no signal is given, it timeout will use the SIGTERM signal to the specified command when the timeout is reached. You can specify which signal to use using the -s ( --signal) option .

For example, to send SIGKILLto a command ping after one minute, you can use the command:

sudo timeout -s SIGKILL ping 8.8.8.8

Signal can be specified by its name like SIGKILLor number like 9. The following command is identical to the previous one:

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 the stuck process

SIGTERM, the default signal sent when the timeout is exceeded may be caught or ignored by some processes. In that situation, the process continues to run after the termination signal is sent.

To make sure the monitored command has stopped, use the option -k ( --kill-after) followed by a time period. When this option is used after the given timeout is reached, the command timeout  sends a signal SIGKILLto the program corresponding to the user’s input.

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

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

The process will be killed after the given time limit is reached even though it is stuck.

Maintaining Exit Status

timeout  will return  124when the time limit has been reached. Otherwise, the command timeout will return the exit status of the managed command.

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

timeout --preserve-status 5 ping 8.8.8.8

Running commands in Foreground

By default, it timeout  runs a command in the background. If you want to run the command in the Foreground, use the option --foreground:

timeout --foreground 5m ./script.sh

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

Conclusion

Command is timeout  used to run a certain command with a time limit according to the will of the user.

Commands  timeout  are simple commands that don’t have many options. Usually you will use a command  timeout  with only two arguments, duration, and the command that will occur.

Be the first to comment

Leave a Reply

Your email address will not be published.


*