How to Timeout a Command in Bash Without Unnecessary Delay


Users can execute various commands and scripts using Bash, a powerful command-line interface. However, some commands may take longer than expected to execute, resulting in unnecessary system delays. To address this issue, the timeout command can be used to limit the amount of time a command runs. This article outlines how to effectively use the timeout command in Bash to timeout a command without any unnecessary delay. For illustrative purposes, we will use the Ubuntu operating system.

Timeout a Command in Bash

We can use Bash’s “timeout” command to time out a command. On certain systems, the “timeout” command is not installed by default; however, on most Linux distributions, it may be done so by using the package manager. The syntax of the “timeout” command is as follows:

timeout [OPTION] DURATION COMMAND [ARGUMENT]...

In this syntax, the timeout command accepts optional OPTION arguments followed by a specified DURATION of time that the COMMAND will run for. The COMMAND parameter specifies the command to be executed, followed by any optional [ARGUMENT]s that can be passed to the command.

The OPTION parameter is optional and allows for additional customization of the timeout command. Some commonly used options include the -s SIGNAL option, which specifies the signal to send to the command if it exceeds the time limit, and the -k DURATION option, which sends a signal to the command after a specified duration of time and terminates the command if it does not exit before that

If, for instance, we wanted to run the “sleep” command for seven seconds but time it out after five seconds, the sample shell script might look like this:

#!/bin/bash

echo "Executing sleep command alongwith timeout of 5 second..."

timeout 5s sleep 7s

echo "Execution of Sleep command completed."

Here, I’ve set the timeout length to 5 seconds and the “sleep” command duration to 7 seconds. Even though the “sleep” command would typically run for 7 seconds, the “timeout” command will terminate it after 5 seconds.

By using the -k option with the timeout command, we can avoid unnecessary delay. If the command runs above the timeout limit, you can specify a signal to be sent to the command to terminate it immediately rather than waiting for it to conclude gracefully. This signal is useful in ensuring that the command doesn’t run indefinitely and that the system resources are freed up for other tasks.

Let’s take the “sleep” command as an example. We might want to execute it for seven seconds, but we also want to timeout the command after five seconds and emit the SIGINT signal if the timeout limit is reached. By executing the following command, we can accomplish this:

#!/bin/bash

echo "running sleep command with timeout of 5 seconds and SIGINT signal after 3 seconds”

timeout -k 2s 3s sleep 5s

echo "execution of sleep command completed."

Here, I’ve set the timeout period to 5 seconds and the signal that will be emitted if the timeout limit is exceeded to SIGINT. The SIGINT signal should be emitted three seconds after the timeout limit has expired, according to the “-k 3s” option.

Text Description automatically generated

Conclusion

The `timeout` command is a valuable tool that enables users to restrict the time a command can run in Bash actively. Users can prevent unnecessary system delays and ensure that commands do not run indefinitely by utilizing the `timeout` command. This command is simple to use, and users can customize it to send various signals to the command when it exceeds the time limit. By following the steps provided in this article, users can efficiently timeout a command in Bash without any unnecessary delay.

Print Friendly, PDF & Email