Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - how does the kill -k switch work in timeout command

Tags:

linux

bash

I have two one liners:

In first. I've expected killing sh -c "..." command after 5 seconds but it exists until the timeout exits (for 10 seconds)

timeout -k 5s 10s sh -c 'sleep 20s && echo "Long running command which is visible under: ps -elf | grep sleep during whole life (10s) time of timeout command"'

In second. I've expected that timeout will exit with return code 124 (because the sh -c "..." command is still running) while the command sh -c "..." will continue to run (because of kill option for timeout was not set)

timeout 10s sh -c 'sleep 20s && echo "Long running command which is visible under: ps -elf | grep sleep during whole life (10s) time of timeout command"'

It seems that argument passed to timeout runs for exact time as timeout command itself (it is not killed earlier nor survive timeout) what is the purpose of kill option then?

like image 521
Wakan Tanka Avatar asked Apr 29 '15 07:04

Wakan Tanka


People also ask

What does timeout do in Linux?

timeout is a command-line utility that runs a specified command and terminates it if it is still running after a given period of time. In other words, timeout allows you to run a command with a time limit.

How do you stop a watch command in Linux?

Linux watch Command Overview By default, the watch command updates the output every two seconds. Press Ctrl+C to exit out of the command output.

What is the kill command in bash?

The command kill sends the specified signal to the specified processes or process groups. If no signal is specified, the TERM signal is sent. The default action for this signal is to terminate the process.


1 Answers

The option -k is to send KILL signal after the specified seconds if the process couldn't be terminated after the timeout.

timeout first sends the TERM signal. If -k is specified, then it'll also send KILL signal, following the real timeout value.

For example

timeout -k 5 10 someCommand

timeout sends TERM signal after the 10 seconds. If someCommand didn't respond to TERM (e.g. it could block the TERM signal) then timeout sends KILL signal after 5 more seconds (i.e. at the 15th second since the start of execution). The signal KILL can't be blocked.

like image 75
P.P Avatar answered Sep 28 '22 13:09

P.P