timeout command in Bash
This article is a simple guide to setting a timeout for a specific program using the timeout command from GNU's coreutils package in Bash.
timeout command in Bash
In many cases, such as fetching data from a server or running a function or program with random input, the program or function may run for a long time or indefinitely.
In this case, it is essential to stop the program and prevent it from wasting more time and resources.
This is where the timeout feature comes in. The user defines a time limit and the program is then allowed to run until it completes successfully, if it takes too long before the user defined time limit is reached then the program is terminated.
Most programming languages usually have a timeout functionality built in. However, in the case of Bash, it is not built-in but a part of an external package called coreutils.
This package is developed and maintained by GNU and contains implementations of many basic utilities.
Simply run the command in your LINUX system sudo apt-get install -y coreutils
to install the coreutils package.
You must first install Homebrew to get coreutils on your macOS system. To get Homebrew, run the following command in a command terminal.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew installation is complete, run the following command.
brew install coreutils
If you have the coreutils package installed you can simply use the timeout function, for example:
timeout 10 ping baidu.com
The ping utility in Bash is built-in and will continue to run until manually interrupted. By default, ping sends one packet every 1 second.
As shown above, the timeout command terminates the ping after 10 seconds because the number after the timeout command is the time limit.
Therefore, the command timeout 10 cmd1 means that cmd1 will run until it completes successfully or until 10 seconds, whichever comes first. The time unit for the timeout command is seconds.
To use time values in minutes, hours, or days, add m, h, or d, respectively.
For example, to set a timeout of 2 minutes:
timeout 2m programToRun
It is important to note that when a process times out, the timeout exit status is 124. In the following example, we will use this to check if the process has timed out.
timeout 1 ping 8.8.8.8 -w3
EXIT_STATUS=$?
if [ $EXIT_STATUS -eq 124 ]
then
echo 'Process timed out!'
else
echo 'Process did not timeout.'
fi
exit $EXIT_STATUS
In the above example, if the process does not complete within 1 second, the process will time out. -w3
The expansion means that the address will be pinged three times.
Therefore, this process will always timeout and give the following output.
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=40.4 ms
Process timed out!
It is important to note that any internal timeout will also result in the same exit status (124). Therefore, this output indicates that the process either expired using the timeout we specified or some internal timeout caused it to terminate.
Using timeouts and SIGKILL signals in Bash
Normally, a timeout function sends a SIGTERM signal to stop the execution of the program when the time limit is reached. The problem is that some programs may ignore the SIGTERM signal and continue to run.
This is where the SIGKILL signal comes in; it immediately stops the execution of a process and all its children, and cannot be blocked or ignored. To use a SIGKILL timeout, we must add the -s flag.
For example:
timeout -s SIGKILL 10 programToRun
When not running directly from the shell timeout
, like when running it from a script, it is important to add --foreground to the command.timeout
Many alternatives to the timeout command are available, all of which are largely based on the same logic of using some termination signal while sleeping and then running the desired command or program which stops once the sleep is over.
example:
(sleep 2 && killall prog) & ./prog
The above code will kill all processes of prog after 2 seconds if prog has not finished executing prog
. Such a solution works, but it is not as safe and reliable as the timeout command, so it is better to use timeout instead.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u