Oct 13, 2023 4 min read

Timeout Command in Linux

Use timeout Command in Linux with our step-by-step tutorial. A command-line utility that terminates a command if it runs past a set time limit.

Timeout Command in Linux
Table of Contents

Introduction

timeout is a command-line utility that runs a command and then terminates it if it is still running after a defined period of time. To put it in another way, timeout allows you to run a command for a set amount of time. The timeout command is included in the GNU core utility package, which comes standard with practically every Linux distribution.

When you wish to run a command that doesn't have a built-in timeout option, it comes in handy.

In this tutorial, we will show you how to utilize the timeout command. We will also address a few FAQs on timeout Command in Linux.

How to Use the timeout Command

The timeout command has the following syntax:

timeout [OPTIONS] DURATION COMMAND [ARG]…

The DURATION can be a positive integer or a floating-point value, with a unit suffix of your choice:

  • s - seconds (default)
  • m - minutes
  • h - hours
  • d - days

It defaults to seconds when no other unit is specified. The linked timeout is deactivated if the duration is set to zero.

Before the parameters, the command options must be specified.

Here are a couple of simple examples of how to utilize the timeout command:

  • Terminate the command after 7 seconds:
timeout 5 ping 8.8.8.8
  • Terminate the command after 7 minutes:
timeout 7m ping 8.8.8.8
  • Terminate the command after one minute and 6 seconds:
timeout 1.1m ping 8.8.8.8

Prefix sudo before timeout if you want to run a command that requires elevated capabilities, such as tcpdump: 

sudo timeout 300 tcpdump -n -w data.pcap

Sending Specific Signal

When the time limit is achieved, timeout delivers the SIGTERM signal to the managed command if no signal is given. The -s (--signal) option allows you to choose the signal to send.

To send SIGKILL to the ping command after one minute, for example, type:

sudo timeout -s SIGKILL 1m ping 8.8.8.8

The signal can be specified by its name, such as SIGKILL, or by its number, such as 9. The following command is the same as the one before it:

sudo timeout -s 9 1m ping 8.8.8.8

Use the kill -l command to acquire a list of all accessible signals:

kill -l

Killing Stuck Processes

Some programs can catch, or ignore SIGTERM, the default signal issued when the time limit is exceeded. After the termination signal is sent in those cases, the process continues to operate.

Use the -k (--kill-after) option followed by a time interval to ensure that the monitored command gets killed. The timeout command delivers the SIGKILL signal to the controlled program when this option is used after the time limit has been achieved. This signal cannot be collected or ignored.

In the following example, timeout executes the command for one minute and then kills it after ten seconds if it is not terminated:

sudo timeout -k 10 1m ping 8.8.8.8

timeout -k “./test.sh”

When the time limit is reached, the process is killed.

Preserving the Exit Status

When the time limit is reached, timeout returns 124. Otherwise, it delivers the managed command's exit status.

Use the --preserve-status argument to keep the command's exit status even if the time limit is reached:

timeout --preserve-status 5 ping 8.8.8.8

Running in Foreground

timeout performs the managed command in the background by default. Use the --foreground argument if you want the command to execute in the foreground:

timeout --foreground 5m ./script.sh

When you wish to perform an interactive command that requires user input, this option comes in handy.

FAQs on timeout Command in Linux

How do I use the timeout command? 

To use timeout, type timeout followed by the desired duration and the command you want to run. For example, timeout 5s command runs the command for a maximum of 5 seconds.

Can the timeout command terminate a command that takes too long to execute? 

Yes, that's precisely the purpose of the timeout command. If a specified command runs longer than the defined time limit, timeout forcibly terminates it.

What happens when a command is terminated by the timeout command? 

When a command is terminated by timeout, it receives a termination signal (SIGTERM). This allows the command to perform any necessary cleanup or termination actions before exiting.

Is it possible to specify the time limit in different formats such as minutes or hours? 

Yes, the timeout command supports multiple formats for specifying time limits. You can use seconds (s), minutes (m), hours (h), or a combination. For example, timeout 2m30s command sets a time limit of 2 minutes and 30 seconds.

Can timeout be used with interactive commands that require user input? 

Yes, timeout can be used with interactive commands. However, since it terminates the command after a set duration, it may interrupt and close interactive sessions that require user input.

Does the timeout command provide any notification when terminating a command? 

By default, timeout does not display any specific notification when terminating a command. However, you can combine it with other scripts or commands to receive notifications or log the termination event if desired.

Is there a way to prevent timeout from killing child processes of the executed command? 

By default, timeout terminates both the main command and any child processes it spawns. However, you can use the --preserve-status option to prevent termination of child processes if needed.

Conclusion

The timeout command is used to perform a command for a set amount of time.

timeout is a straightforward command with few arguments. In most cases, you'll just give timeout two arguments: the duration and the managed command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.