Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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)

share|improve this answer was recently fixed, so there should be an updated libvirt soon.

share|improve this answer
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 '11 at 7:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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