Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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"
    
    task
    
    script
        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.

share|improve this answer
add comment

Your Answer

 
discard

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.