sudo timeout -vk 5 10 kill PIDhere
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 (if kill
was actually hanging, killing it would not affect your actual process). timeout
is 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:
$ some_process &
# time passes, eventually we decide to terminate the process
$ kill %1
$ sleep 5s
$ kill -s SIGKILL %1 # will fail and do nothing if %1 has already finished
Or you could rely on wait
which will return early if the job terminates before the sleep
completes:
$ some_process &
# time passes
$ kill %1
$ sleep 5s &
$ wait -n %1 %2 # returns once %1 or %2 (sleep) complete
$ kill -s SIGKILL %1 # if %2 completes first %1 is still running and will be killed
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.
if there is better command that even work with part of the name
Does pkill
do what you want?