Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Automatically shutdown KVM Virtual Machines

Currently, there’s no built-in tool to properly shutdown VMs  when the KVM host does. So after couple of hour “googling” it I got some idea from this link, made some changes to make simplier. Here’s my version:


LIST_VM=`virsh list | grep running | awk '{print $2}'`
DATE=`date -R`

if [ "x$activevm" =  "x" ]
 exit 0

for activevm in $LIST_VM
 PIDNO=`ps ax | grep $activevm | grep kvm | cut -c 1-6 | head -n1`
 echo "$DATE : Shutdown : $activevm : $PIDNO" >> $LOGFILE
 virsh shutdown $activevm > /dev/null
 while [ "$COUNT" -lt "$TIMEOUT" ]
ps --pid $PIDNO > /dev/null
if [ "$?" -eq "1" ]
sleep 5
if [ $COUNT -lt 110 ]
echo "$DATE : $activevm not successful force shutdown" >> $LOGFILE
virsh destroy $activevm > /dev/null
  1. Save the code in /etc/init.d/shutdownvm
  2. Then make it an executable file
    chmod 755 /etc/init.d/shutdownvm
  3. Create links to both rc0.d and rc6.d
    cd /etc/rc0.d ln -s ../init.d/shutdownvm K18shutdownvm cd /etc/rc6.d ln -s ../init.d/shutdownvm K18shutdownvm
  4. Done.

5 responses to “Automatically shutdown KVM Virtual Machines

  1. hardc0l2e November 4, 2010 at 6:29 am


    On Ubuntu 10.04, adding shutdown init for shutdownvm.sh is not working anymore. You should add a pre-stop script in libvirt-bin and add shutdown init for it.

    1. Add the following in /etc/init/libvirt-bin.conf:

    pre-stop script
    end script

    2. Then enlist libvirt-bin in the shutdown and reboot process.

    update-rc.d libvirt-bin stop 10 0 6 .

    3. Done.

  2. Mike April 29, 2011 at 7:29 pm

    Howdy, thanks for sharing your script.

    I think this line:

    if [ "x$activevm" = "x" ]

    Should be

    if [ "x$LIST_VM" = "x" ]

    since $activevm will always be empty at that point in the script

  3. Mike April 29, 2011 at 7:48 pm

    Also, in CentOS 5.6 (not sure if this script was in 5.5) there is a script that suspends the KVM guests on host shutdown:


    Here’s the description

    # This is a script for suspending active libvirt guests \
    # on shutdown and resuming them on next boot

  4. homer June 28, 2011 at 6:27 am

    I´ve Ubuntu 10.04.2 LTS,

    and take the script from top. I make my links on rc0.d and rc6.d, too.

    cd /etc/rc0.d
    ln -s ../init.d/shutdownvm S00shutdownvm
    cd /etc/rc6.d
    ln -s ../init.d/shutdownvm S00shutdownvm

    I add not a pre-stop script in ‘libvirt-bin.conf’ AND ‘qemu-kvm.conf’.
    But in both files I add the numbers of the ‘stop on runlevel’.

    for example:
    start on runlevel [2345]
    stop on runlevel [!023456]

    That´s all of my configure.

    P.S. I´ve add ‘….| head -n1’ in line 15 of the script. Because I had problems with
    similar names of my VMs.

    for example:
    VM1: Maschine
    VM2: Maschine-xy

  5. Stuart January 1, 2015 at 7:26 am

    A better way to exit early if no vm’s are running:

    if [ -z “$LIST_VM” ]; then
    exit 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: