I have libvirt/KVM set up on my Ubuntu and before shutting down the machine I would like it to attempt shutting down the VM's using an ACPI poweroff (virsh shutdown), then I want it to stop shutdown for at least 60 seconds to give the VM's a chance to sync everything to disk, this is what I have:

pre-stop script
    for RUNNING_VM in `virsh list | grep -E running | awk -F" " '{ print $2 }'`
        echo "Shutting down ${RUNNING_VM} on `date`" >> $SHUTDOWN_LOG
        virsh shutdown ${RUNNING_VM}
    echo -n "Waiting for VM's to shut down: " >> $SHUTDOWN_LOG
    for I in `seq 1 10`
        RUNNING=`virsh list | grep running | wc -l`
        if [[ "$RUNNING" == "0" ]] 
            echo "done." >> $SHUTDOWN_LOG
            exit 0
            /bin/sleep 6 && echo -n " ${RUNNING} " >> $SHUTDOWN_LOG
    echo " Cleaning up..." >> $SHUTDOWN_LOG
end script  

The issue I am having is that for some odd reason it never gets to echo "done." even if the VM's are all shut down correctly, it will keep looping and thus stalling for 60 seconds, OR it seems to completely ignore the sleep, writes the total to the $SHUTDOWN_LOG and quits immediately anyway in the middle of my VM's syncing to disk.

Shutting down FreeBSD-services on Mon Aug 22 02:07:42 MDT 2011
Shutting down FreeBSD-pgsql on Mon Aug 22 02:07:42 MDT 2011
Waiting for VM's to shut down:  1 [EOF]

Is the log output ... is there any better way to log from upstart?

2 Answers 2


https://bugs.launchpad.net/ubuntu/lucid/+source/libvirt/+bug/350936 was recently fixed, so there should be an updated libvirt soon.

  • This is fantastic news ... I ended up writing a SysV script that ran before sendsigs to shut down the VM's and replaced stop on with never stopping it. So libvirt eventually gets hard killed when the VM's are shut down. Worth it.
    – X-Istence
    Sep 10, 2011 at 7:41

You can't echo from an upstart script unfortunately, the best option is the one you're already doing which is creating your own log.

If you want to also output to syslog you can do this in upstart using logger (as you would in init.d) but Upstart debugging so far is pretty harsh.

You can also add set -x at the beginning of your pre-stop script to get more output through the default upstart log channel (syslog in Ubuntu)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.