Suspend running vmware and switch the esx(i) off

I have been using a script for a while switching off the esxi to a certain time. Crontab in used to do it, as this post

Unfortunately, this script does not do much than poweroff the vms and switching off the esxi; As a consequence the vmware’s running at that time would have been stopped roughtly;

All the work done would have been lost, so I decided to review this script to do the following;

– List of Machines currently running
– Retrieve ID’s for each Machines
– suspend command for each IDs.
– Then Poweroff the Esx(i).

This is script that I am currently running that suspend all running vms then the esx(i) is switched off.

I am more than convince that there is room for improvement, so any comments are more than welcome 🙂

#! /bin/ash

rm -f listid
touch listid


######## Listing the running vms################
esxcli vm process list |grep -v "^\s.*"| grep -v "^$" > list

########## cleaning the id.s file by keeping only the id
for name in `cat list`;do
        vim-cmd vmsvc/getallvms | grep $name | grep vmx | grep -v "^$" | awk '{print $1}'>> listid
done

for id in `cat listid`;do
        ###### suspending vms##########
        echo "Suspending the running machines"
        vim-cmd vmsvc/power.suspend $id

done

###### removing files############
#
rm list
rm listid



echo "done."
echo "shutting down the host now.."
/bin/poweroff

Leave a Reply