3 Replies Latest reply on Sep 12, 2008 5:05 AM by espi3030

    Script to Shutdown and startup of virtual machines

    as00201 Lurker

      I need a script to shutdown and startup virtual machines from the ESX 3.5 console. Please also include how to run the script and also I have a few examples but not sure which is the best route to take see beloW: These were all taken from various sites of the internet and I am not sure what to do with either of them any help is greatly appreciated

       

      (option 1)

      Here is a bash script to power off all active VMs on a host.

      #!/bin/bash

      • IFS='*

      • '*

      • for vm in `vmware-cmd -l`*

      • do*

      • name=`vmware-cmd "$vm" getconfig displayname -q`*

      • state=`vmware-cmd "$vm" getstate -q`*

      • if "$state" == "on" *

      • then*

      • echo "Trying to power off $name...."*

      • output=`vmware-cmd "$vm" stop trysoft`*

      • if $? -eq 0 ; then*

      • echo "$name was powered off successfully."*

      • fi*

      • fi*

      • done*

       

      (option 2)

      VMLIST=`vmware-cmd -l`

      for config in $ do vmware-cmd $ stop trysoft

      done

      vimsh -n -e /hostsvc/maintenance_mode_enter

      shutdown -h now

       

       

      (option 3)

      root@esxhost root--# vmware-cmd /vmfs/volumes/44ebf538-51cc7998-2525-00145e1b556a/printer/printer.vmx stop trysoft

        • 1. Re: Script to Shutdown and startup of virtual machines
          Texiwill Guru
          User ModeratorsvExpert

          Hello,

           

          Any of those options is fine. Note however that you may need to use 'soft' instead of 'trysoft' if this is through the RCLI. Also, to start its the reverse using 'start' instead of 'trysoft'.

           

          Note that the second script also enters the system into maintenance mode.

           

          What are you ideally trying to do?

           


          Best regards,

          Edward L. Haletky

          VMware Communities User Moderator

          ====

          Author of the book 'VMWare ESX Server in the Enterprise: Planning and Securing Virtualization Servers', Copyright 2008 Pearson Education.

          CIO Virtualization Blog: http://www.cio.com/blog/index/topic/168354

          As well as the Virtualization Wiki at http://www.astroarch.com/wiki/index.php/Virtualization

          • 2. Re: Script to Shutdown and startup of virtual machines
            dconvery Virtuoso
            vExpert

             

            Check out Duncan's script. It will shutdown the ESX too, so you may want to remove that part ->http://www.yellow-bricks.com/2008/06/16/shutdown-all-vms-and-esx/

             

             

            Dave

             

             

            • 3. Re: Script to Shutdown and startup of virtual machines
              espi3030 Expert

               

              hello as00201,

               

               

               

               

               

              Here is a shutdown script i found a while back:

               

               

               

               

               

              1. set the paths that the vmware tools need

              PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

              1. try do a nice shutdown of VM there is power

              count_vm_on=0

              for vm in `vmware-cmd -l` ; do

                #echo "VM: " $vm

                for VMstate in `vmware-cmd "$vm" getstate` ; do

                #echo $VMstate

               

               

                 

              1. If the VM is power ON

                if [ $VMstate = "on" ] ; then

                   echo " "

                   echo "VM: " $vm

                   echo "State: is on and will now tell it to shut down"

                   echo "Shutting down: " $vm

                   vmware-cmd "$vm" stop trysoft

                   vmwarecmd_exitcode=$(expr $?)

                   if ; then

                       echo "exitcode: $vmwarecmd_exitcode so will now turn it off hard"

                       vmware-cmd "$vm" stop hard

                   fi

                   count_vm_on=$count_vm_on+1

                  sleep 2

                 

              1. if the VM is power OFF

                elif [ $VMstate = "off" ] ; then

                   echo " "

                   echo "VM: " $vm

                   echo "State: is off, so i skip it"

                 

              1. if the VM is power suspended

                elif [ $VMstate = "suspended" ] ; then

                   echo " "

                   echo "VM: " $vm

                   echo "State: is suspended, so i skip it"

                 

              1. if state is getstate or =

                else

                   printf ""

                  #echo "unknown state: " $VMstate

                fi

               

               

                done

              done

               

               

               

              ########################################################################

              1. wait for up to 5 min for the VM to shutd

              #

              if ; then

                echo " "

                echo "All VM is off or suspended"

              else

                echo " "

                vm_time_out=300

                count_vm_on=0

                echo "Waiting for VMware virtual machines."

                for (( second=0; second<$vm_time_out; second=second+5 )); do

                   sleep 5

                   printf "."

                   count_vm_on=0

                   for vm in `vmware-cmd -l` ; do

                      for VMstate in `vmware-cmd "$vm" getstate` ; do

                      if [ $VMstate = "on" ] ; then

                         count_vm_on=$(expr $count_vm_on + 1)

                      fi

                   done

                done

                if ; then

                   #echo "exit for"

                   break

                fi

                done

                #echo $VMstate

              fi

              #echo $count_vm_on

               

               

               

              #####################################################################

              1. checking if all the VM are off and if not then turn them off

              for vm in `vmware-cmd -l` ; do

                #echo "VM: " $vm

                for VMstate in `vmware-cmd "$vm" getstate` ; do

                   

              1. If the VM is power ON

                  if [ $VMstate = "on" ] ; then

                    echo " "

                    echo "Found this VM: " $vm

                    echo "it is stille on but now i will turn it off"

                    vmware-cmd "$vm" stop hard

                    sleep 2

                  fi

                done

              done

               

               

               

              #####################################################################

              1. Will now shutdown the ESX server as all VM are now off

              echo " "

              echo "now all VM(s) are down, so will shutdown ESX host now...  Good bye"

              echo " "

              shutdown -h now