Commit 818fd9c7 by Christian Seiler Committed by Stéphane Graber

lxc-shutdown: Make all processes exit before timeout if shutdown works

The following rationale is for using the -t option: Currently, lxc-shutdown uses a subprocess for the timeout handling, where a 'sleep $TIMEOUT' is executed, which will kill the main process after the timeout has occurred, thus causing the main process to stop the container hard with lxc-stop. On the other hand, if the timeout is not reached, the main process kills the subprocess. The trouble now is that if you kill a shell that is running in the background, the kill will only take effect as soon as the program currently running in the shell exits. This in turn means that the subprocess will never terminate before reaching the timeout. In an interactive shell, this does not matter, since people will just not notice the process and lxc-shutdown returns immediately. In a non-interactive enironment, however, there may be circumstances that cause the calling program to wait until even that subprocess is terminated, which means that shutdown will always take as long as the timeout, even if the container shuts down quite a bit earlier. This change makes sure that also all subprocesses of the background process are killed from the main process. This will immediately terminate the background process, thus ensuring the desired behaviour. Signed-off-by: 's avatarChristian Seiler <christian@iwakd.de> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent f3ca99fd
...@@ -131,7 +131,7 @@ fi ...@@ -131,7 +131,7 @@ fi
if [ $timeout != "-1" ]; then if [ $timeout != "-1" ]; then
trap dolxcstop EXIT trap dolxcstop EXIT
alarm $$ $timeout & alarm $$ $timeout 2>/dev/null &
alarmpid=$! alarmpid=$!
fi fi
...@@ -141,7 +141,9 @@ done ...@@ -141,7 +141,9 @@ done
if [ $timeout != "-1" ]; then if [ $timeout != "-1" ]; then
trap - EXIT trap - EXIT
kill $alarmpid # include subprocesses; otherwise, we may have to wait until sleep completes
# if called from a non-interactive context
kill $alarmpid $(ps --no-headers --ppid $alarmpid -o pid) 2>/dev/null || :
fi fi
echo "Container $lxc_name has shut down" echo "Container $lxc_name has shut down"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment