Using the timeout command (with examples)
The timeout
command is a powerful tool that allows you to run other commands with a time limit. It is particularly useful when you want to restrict the execution time of a command to prevent it from running indefinitely or to handle time-sensitive operations. In this article, we will explore different use cases of the timeout
command and provide code examples to illustrate each case.
Use case 1: Terminating a command if it exceeds a specified duration
Suppose you want to run the sleep
command, which pauses the execution for a specified number of seconds, but you want to ensure that it does not run for more than a certain duration. You can achieve this by using the timeout
command with the following code:
timeout 3s sleep 10
Motivation for using this example:
This example demonstrates how to set a time limit for a command to terminate if it runs for more than a specified duration. By providing a timeout value of 3 seconds to the timeout
command, we ensure that the sleep
command will be terminated if it runs for more than 3 seconds.
Explanation of the arguments:
timeout
: The main command that executes other commands with a time limit.3s
: The duration after which the command should be terminated. In this case, we specify 3 seconds as the timeout value.sleep 10
: The command that we want to execute with a time limit. In this example,sleep 10
is the command that pauses the execution for 10 seconds.
Example output:
When the timeout
command is executed with the provided code, the sleep
command is terminated after running for 3 seconds. As a result, the total execution time is limited to 3 seconds, even though the sleep
command itself was set to sleep for 10 seconds.
Use case 2: Specifying the signal to be sent after the time limit expires
By default, the timeout
command sends the TERM signal to terminate a command after the time limit expires. However, you can specify a different signal to be sent. Here’s an example:
timeout --signal INT 5s sleep 10
Motivation for using this example:
This example demonstrates how to customize the signal sent to a command when the time limit elapses. In this case, we specify the INT signal to be sent after 5 seconds of executing the sleep
command.
Explanation of the arguments:
--signal INT
: This argument specifies the signal to be sent when the time limit is reached. In this example, we use the INT signal (equivalent to pressing Ctrl+C) to terminate thesleep
command.5s
: The duration after which the command should be terminated. In this case, we specify 5 seconds as the timeout value.sleep 10
: The command that we want to execute with a time limit. In this example,sleep 10
is the command that pauses the execution for 10 seconds.
Example output:
When the timeout
command is executed with the provided code, the sleep
command is terminated after running for 5 seconds. Instead of the default TERM signal, the INT signal is sent, resulting in the termination of the sleep
command.
These two use cases demonstrate the flexibility and usefulness of the timeout
command in managing the execution time of other commands. Whether you need to enforce time limits or customize the termination signal, the timeout
command provides a simple and effective solution.