In Linux operating system, some processes run continuously, and to kill them after a certain amount of time a command-line utility called “timeout” is used.

The “timeout” allows the user to execute a command and then terminate it after a set time has passed. In other words, “timeout” makes it easier to run a command for a certain amount of time. The “timeout” command is included in the GNU core utility kit, which comes standard with almost every Linux system.

In the write-up, we will go through the fundamentals of this command with examples.

Syntax

The “timeout” command has the following syntax:

timeout [option] [Duration] [command]

Duration can be in floating integers with time suffix:

s/no suffix (when no suffix unit is fixed it consider to second)

m= minute

h= hour

d= day

Using the “timeout” Command

Ping your system with an IP address, it will keep on going until you terminate it by pressing CTRL C.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-85.png" data-lazy- height="494" src="data:image/svg xml,” width=”799″>

Now, set a time to terminate it using the “timeout” command. For instance, I will set the time to 7, which means the execution of the “ping” command will stop after 7 seconds.

Follow the below-given command to set the time limit:

$ timeout 7 ping 192.168.18.213

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-87.png" data-lazy- height="222" src="data:image/svg xml,” width=”800″>

  • Setting Timeout with Another Time Unit

Similarly, we can ping “facebook.com” as well, using:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-89.png" data-lazy- height="392" src="data:image/svg xml,” width=”792″>

To stop the “ping” command after 1 minute, type a below-given command in a terminal:

$ timeout 1m ping facebook.com

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-91.png" data-lazy- height="293" src="data:image/svg xml,” width=”730″>

  • Showing Exit Status

Many programs sent a value/code to the shell when they terminate.

For example, terminate the following process before it hit its time limit that is 6 seconds. Hit CTRL C and check exit status by typing “echo $”. It will return “0” in the output:

$timeout 6 ping facebook.com

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-93.png" data-lazy- height="329" src="data:image/svg xml,” width=”796″>

Interrupting a running process will always give a “0” exit code.

  • Preserve the Status

Preserve status returns an exit status even if we terminate it forcefully using CTRL C. The command would be:

$ timeout –preserve-status 2 ping facebook.com

And to check the exit code, type:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-94.png" data-lazy- height="225" src="data:image/svg xml,” width=”791″>

Now, the exit status will be 143.

  • Sending Signals to Timeout

The “timeout” command sends a signal to the process to terminate it.

To check the available signals use the “kill” command:

A list of all available signals will be displayed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-97.png" data-lazy- height="344" src="data:image/svg xml,” width=”727″>

The “-s” (–signal) option allows you to set a signal to give:

The “timeout” command uses the “SIGTERM” to stop a process, but many processes ignore the “SIGTERM” signal. To forcefully terminate a process using the “SIGKILL” signal, cannot be ignored by any process.

Sending SIGKILL to the ping command after 2 seconds, type:

$ sudo timeout -s SIGKILL ping facebook.com

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-98.png" data-lazy- height="78" src="data:image/svg xml,” width=”731″>

You can also send signals by using a signal number instead of a signal’s name:

$ sudo timeout -s 9 ping 192.168.18.213

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-100.png" data-lazy- height="78" src="data:image/svg xml,” width=”783″>

  • Sending Kill Signals

The “-k” option is being used to terminate the process.

In the following example, if the connection does not establish in 2 minutes, it will kill the timeout command after 3 seconds:

$ sudo timeout –k 3 2m ping facebook.com

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-102.png" data-lazy- height="253" src="data:image/svg xml,” width=”733″>

In my case, the timeout command will run for 2 minutes and not be terminated. Therefore, it will not kill even after 3 seconds.

  • Getting Help

Linux has a very good feature that it provides help for every command we use. By using “help” with a command name, it will give you the help information in detail:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-103.png" data-lazy- height="521" src="data:image/svg xml,” width=”793″>

  • Checking Version

To check the version of the timeout command, type:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-104.png" data-lazy- height="203" src="data:image/svg xml,” width=”799″>

Conclusion

In Linux operating system, the “timeout” command is a command-line utility that is used to terminate a running process after a set period. It is used for the processes that run continuously. Moreover, the exit status of running processes can also be accessed using the “timeout” command. We have discussed timeout commands through different examples with several options.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/4255bac0cf82c6bdb0e1445cec1dfeb9?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Linux Explorer

Linux Explorer — Always exploring Linux and learning to advance the state of the art.