JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

timeout command in Bash

Author:JIYIK Last Updated:2025/03/21 Views:

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 coreutilsto 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. -w3The 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.

Article URL:

Related Articles

Check if input parameter exists in Bash

Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM

When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh

Start a new terminal session in Bash

Publish Date:2025/03/21 Views:51 Category:OPERATING SYSTEM

In various situations when using Bash or another shell, you may need to run a script or program in a new terminal instance or in another tab in the same terminal. Opening a new terminal instance or tab from within Terminal is simple; we'll

Checking Syntax in Bash

Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM

This article discusses methods for checking Bash scripts for syntax problems without running the script. Syntax errors are caused by some grammatical or spelling mistakes in the code. In modern compilers of other programming languages, the

Run batch (.bat) files in CMD

Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM

This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go

Changing CMD text color using batch script

Publish Date:2025/03/20 Views:172 Category:OPERATING SYSTEM

This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr

Batch set command timeout

Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM

This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a

Batch merge XML files

Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM

This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku

Run .exe file from command prompt using batch script

Publish Date:2025/03/20 Views:77 Category:OPERATING SYSTEM

This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an

Deleting files older than N days using batch script

Publish Date:2025/03/20 Views:57 Category:OPERATING SYSTEM

In this article, we will use a batch script to delete files older than N days. Deleting files older than N days using batch script The general format of the code to perform this task is shown below. FORFILES /p "D:\DIRECTORY" /S /M *.* /D -

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial