How To Use “Timeout” Command In Linux?

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.

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

  • Setting Timeout with Another Time Unit

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

$ ping facebook.com

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

$ timeout 1m ping facebook.com

  • 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

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:

$ echo $?

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:

$ kill -l

A list of all available signals will be displayed.

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

$ timeout -s <command>

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

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

  • 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

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:

$ timeout --help

  • Checking Version

To check the version of the timeout command, type:

$ timeout --version

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.



from Linux Hint https://ift.tt/3hAune2

Post a Comment

0 Comments