How should look the Linux command to send terminate signal to the process/PID and if it fails to exit gracefully after 10 seconds kill it?
My attempt is: "sudo timeout -vk 5 10 kill PIDhere" (-v verbose, -k kill after X seconds) but I am not sure if it is good or how to adjust values or if there is better command that even work with part of the name shown in process COMMAND line. ("ps aux" output)
Will execute
kill, and then attempt to terminate that process if it takes too long. Which shouldn't happen, and presumably isn't what you want (ifkillwas actually hanging, killing it would not affect your actual process).timeoutis useful for capping how long a process runs for, not how long it takes to terminate after receiving a signal.Instead, I'd suggest starting the process asynchronously (e.g. using
&in a shell, but any language's subprocess library will have similar functionality) and then waiting for the process to terminate after you send it a signal. I describe doing this in Java in this answer. In the shell that might look like:Or you could rely on
waitwhich will return early if the job terminates before thesleepcompletes:You can do the same as above with PIDs instead of job IDs, it's just a little more fiddly because you have to worry about PID reuse.
Does
pkilldo what you want?