How to Timeout a Command in Bash Without Unnecessary Delay

How to Timeout a Command in Bash Without Unnecessary Delay

Learning how to timeout a command in Bash without unnecessary delay can be critical for programming. We’re here to help you understand just that. 

Sometimes, a command in bash may take a long time to complete. But, you may not want to wait so long for the command to finish execution before the command. 

The solution to this problem is to timeout the command. Using the timeout command, we can ensure that the command doesn’t take more than the specified time limit for its execution.

We will discuss the need for a timeout function. Then, we discuss various examples of timeout commands, including how to timeout a command in Bash without unnecessary delay. Finally, we will discuss how to create our own timeout function.

Why do we need the timeout command?

The computers we use daily have minimal processing capability, and the processes we run in a system are not supposed to run forever. We use timeouts to avoid situations in which a program can run indefinitely. 

bashfile command

Timeout exists on different levels in the computing arena. We need to decide on the following aspects for a timeout:

  • The timeout value, i.e., the time we should wait.
  • We then wait for the timeout and check if the process is still running.
  • We can send a termination signal to the process.

Reasons for timeout

There can be multiple reasons a process may run indefinitely. Below we discuss some of the possible causes.

Hardware reasons

For instance, a process is unable to complete due to hardware failure. There can be corrupt files the process tries to read or a network failure leading to packet failures. There can also be hardware limitations, such as low memory or thrashing.

Software reasons

The bugs in the development of a process may also lead to the indefinite execution of a process. For instance, inappropriate handling of threads may lead to deadlocks. A simple infinite loop in a program can lead to a process running for an endless amount of time.

How to Timeout a Command in Bash without Unnecessary Delay

Following is the usage syntax of the timeout command:

timeout [OPTION] DURATION COMMAND [ARG]


By using the OPTION, you can control the behavior of the timeout command. You can specify the time limit for running the command using the DURATION parameter. COMMAND is the actual command you want to run, and ARG is the command line argument that you may pass to the command.

We will now discuss various examples of timeout commands. To follow the tutorial, you will need a computer with Linux and a timeout command installed.

Example 1- Timeout an infinite loop

For instance, the following timeout command will stop the infinite loop after the specified period:

$ timeout 1s bash -c 'for((;;)); do :; done'
$ echo $?
124


Note down the exit code. The process was stopped by a termination signal SIGTERM

Example 2- Preserving the status code of the original process

We can also preserve the status code of the original process using –preserve-status. The following example illustrates it:

$ timeout --preserve-status 1s bash -c 'exit 66'
$ echo $?
66


By using this option, we can identify the code causing the actual issue.

Example 3- Timeout the VLC command

The following command illustrates how we can timeout a VLC command. 

$timeout 10s vlc -vvv http://192.168.0.13:8080/stream.mjpg --sout="#std{access=file,mux=ogg,dst=/home/whsrobotics/vlc_project/first_try.mp4}"


The VLC command will run for 10 seconds and then stops. 

Note: We may also use the VLC command in the background using “&”.

Example 4- Timeout the sleep command

For example, we will discuss how to timeout the sleep command. Suppose you have set the sleep command to sleep for 5 seconds. Now, we will timeout the sleep command after three seconds. The following example illustrates it:

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


In the above program, we have run the sleep command for 5 seconds. But we have specified the timeout of 3 seconds, and the timeout will ensure the program stops after 3 seconds instead of 5 seconds. Following is a sample output:

bashfile timeout

Example 5 – How to Timeout a Command in Bash without unnecessary delay

We can prevent unnecessary delay with the timeout command. This will send a signal to the command if it exceeds the time limit. The command will stop immediately by passing the signal instead of finishing gracefully. We modified the script in the previous section with a signal:

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


In the above example, we have specified the timeout limit of 3 seconds. However, -k 2s specifies that a SIGINT signal will be sent to the sleep command after 2 seconds.

Developing your custom timeout function

In addition to the timeout command available in Linux, we may create our own custom timeout command. The following example illustrates it:

custom timeout command


The program accepts the timeout value as an argument. The program then runs the command in the background. The program uses the sleep function to wait for the specified time.

The program finally checks if the process is still running using its PID. If the process is still running, the process is stopped using the kill command.

In this article, we have discussed how to timeout a command in Bash without unnecessary delay and the reasons for using it. We also explored various examples of the timeout command and demonstrated how to develop a custom timeout function.

By learning how to use the timeout command effectively, you can improve your programming skills and avoid potential issues caused by long-running processes. 

If this guide helped you, please share it.

Related Posts