BASH Programming

How to Timeout a Command in Bash Without Unnecessary Delay

In Bash, sometimes you may need to run a command that takes a long time to complete, and you may not want to wait for it to finish indefinitely. One solution to this problem is to use a timeout command that limits the amount of time a command can run. This article, will discuss how to timeout a command in Bash without unnecessary delay.

Timeout a command in Bash

To timeout a command in Bash, we can use the “timeout” command. The “timeout” command is not available by default on all systems, but it can be installed using the package manager on most Linux distributions, here is the syntax of the “timeout” command:

timeout [OPTION] DURATION COMMAND [ARG]

 
Here the “OPTION” is an optional argument that specifies the behaviour of the timeout command, “DURATION” is the time limit for the command to run, and “COMMAND [ARG]” is the command and its arguments that we want to run.

For example, let’s say we want to run the “sleep” command for five seconds, but we want to timeout the command after three seconds and here is the example shell script:

#!/bin/bash
echo "Starting sleep command with timeout of 3 seconds..."
timeout 3s sleep 5s
echo "Sleep command finished."

 
Here I have specified the duration of the timeout as 3 seconds, and the duration of the “sleep” command as 5 seconds. The “timeout” command will stop the “sleep” command after 3 seconds, even though the “sleep” command would normally run for 5 seconds.


To prevent unnecessary delay when using the “timeout” command, we can use the “-k” option. The “-k” option specifies a signal that will be sent to the command if it exceeds the timeout limit. This signal will cause the command to terminate immediately, instead of waiting for it to finish gracefully.

For example, let’s say we want to run the “sleep” command for five seconds, but we want to timeout the command after three seconds and send the SIGINT signal if it exceeds the timeout limit. We can do this by running the following command:

#!/bin/bash
echo "Starting sleep command with timeout of 3 seconds and SIGINT signal after 2 seconds”
timeout -k 2s 3s sleep 5s
echo "
Sleep command finished."

 
Here I have specified the timeout duration as 3 seconds and the signal to be sent as SIGINT if it exceeds the timeout limit. The “-k 2s” option specifies that the SIGINT signal should be sent after two seconds of the timeout limit.

Conclusion

Timeout a command in Bash is a useful tool that can help you run commands more efficiently and prevent unnecessary delays. By using the “timeout” command and the “-k” option, you can limit the amount of time a command can run and send a signal to terminate it immediately if it exceeds the timeout limit. This will help you save time and run your scripts more efficiently.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.