简体   繁体   中英

kill & wait in one step

If I use a combination to kill a child process in batch and wait for it's termination, I use

kill $PID
wait $PID

If the process exists immediately, the wait will fail, since the pid is not running anymore.

Is there a way to combine both statements to a single one to aviod the error?

Edit: The process I have to kill uses a tempfile; thus it has to be closed (and not just signaled to close) to start it again. Checking the return value of kill does not help, since this indicates whether the signal was delivered successfully.

It's not a one-liner, but would you be willing to consider spawning off the kill with a short sleep, then waiting in the main thread? Something like:

(sleep 1; kill $PID) &
wait $PID

This addresses your concern of the PID being reused after the kill. Even if you reduce the sleep to something much smaller, it introduces idle time, but it should at least ensure that you wait on the correct process.

Effectively, there is not an atomic kill and wait since they are two separate system calls. Some wrapper must be written to execute both functions.

If you do not care about the status,

kill $PID 2> /dev/null
wait $PID 2> /dev/null

If you do care about the status, but do not want an error message, do something like

if ! kill $PID 2> /dev/null; then
    # Error logic here
fi

The fancy way for both is a function

killAndWait() {
    kill $1 2> /dev/null && wait $1 2> /dev/null
}

Then, do

killAndWait $PID
kill $PID wait $PID

If the process exists immediately, the wait will fail, since the pid is not running anymore.

As long as $PID really points to a child process of the shell, I don't think wait will fail. I don't see an error with your code.

Experiment:

bash-3.2$ while : ; do ls > /dev/null ; done &
[1] 44908
bash-3.2$ kill 44908
[1]+  Terminated: [...]
bash-3.2$ wait 44908
bash-3.2$ echo $?
143

143 is the return code for SIGTERM, so the kill worked as expected, and Bash could wait for the dead process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM