I've written this code to automatically start KVM Virtual Machine at boot of host system, and stop Virtual Machine at shutdown or reboot of host system.

start on startup
start on started networking and started qemu-kvm and started libvirt-bin
stop on runlevel [016]

kill timeout 70
expect fork

pre-start exec /KVM/luc.sh prepara

exec /KVM/luc.sh avvia

pre-stop script 
   /KVM/luc.sh ferma
   /bin/sleep 20
end script

luc.sh is a script with these functions:

  • prepara() - prepare the environment
  • avvia() - start KVM
  • ferma() - shutdown KVM

It works except for the sleep command. I need to delay the shutdown/reboot operation of the host system to wait for the guest shutdown to complete. In other words, I want insert a wait of ~20 sec after the /KVM/luc.sh ferma command. It seems that the sleep call in the upstart script is asynchronous. Is it possible to make a synchronous call in the upstart script?

1 Answer 1


Yes, somehow, sleep doesn't work for me either (I'm using Ubuntu 10.04). I'm not sure that the following solution is the best one but it seems to be working.

  1. Create an additional upstart task, say, post-sleep.conf (logging added for clarity):

    description "Sleep when the job finishes"
        echo "Sleeping..." | logger -t "post-sleep-job"
        /bin/sleep 60s
        echo "Done sleeping" | logger -t "post-sleep-job"
    end script
  2. Add the following to the job that needs to sleep after shutdown:

    post-stop script
        start post-sleep
    end script

Use instances in the first script if several jobs need to wait before full shutdown. However, I think an even better and more robust solution would be to rely on events.

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.