I was able to test NUT executing a shell script /sbin/doshutdown twice. I tried a couple different directories, but /sbin was the most consistent and survived reboots. I have 3 ESXI servers that all need to be gracefully shutdown before FREENAS since FREENAS provides the datastore. These scripts were modified from the versions found
here.
The following script does a root ssh into each server and executes a script to shutdown the VMs and then ESXI. I do not run the process in the background (i.e. &) since I need the process to wait until all VMs are shutdown.
*****************************
#!/bin/sh
WALL=wall
# this script is triggered in case the power fails
# configured ssh-automatic-access with keys
echo "Shutting Down all VMs on SERVER01..." | ${WALL}
ssh root@10.1.10.1 "nohup /vmfs/volumes/datastore01/scripts/shutdown_esxi.sh"
echo "Shutting Down all VMs on SERVER02..." | ${WALL}
ssh root@10.1.10.2 "nohup /vmfs/volumes/datastore02/scripts/shutdown_esxi.sh"
echo "Shutting Down all VMs on SERVER03..." | ${WALL}
ssh root@10.1.10.3 "nohup /vmfs/volumes/datastore03/scripts/shutdown_esxi.sh"
echo "finished shutting ESXI down..." | ${WALL}
sleep 300 # Wait 5 minutes for ESXI Servers to Completely Shutdown
echo "Shutting Down BACKUP FREENAS Server..." | ${WALL}
ssh root@10.1.10.4 "nohup /sbin/shutdown -p now"
echo "Shutting Down FREENAS Server..." | ${WALL}
/sbin/shutdown -p now
**************************
The following script runs on each ESXI host to shut the VMs and Host down. The process on FREENAS will wait until this script completes on each ESXI host before moving on. This will shutdown each VM that has VMWare Tools installed. It does not rely on having VMs setup in the Auto Startup/Shutdown setting.
**********************************
#/bin/sh
##########################################################################################################################
#courtesy of
http://www.c-note.dk/2011/12/04/wmware-esxi-suspend-all-guests/
#
#copy this file somewhere to datastore
#e.g. ‘/vmfs/volumes/myDataStore/scripts/’
#
# looks which vm's are running, sends them into Shutdown, waits until Shutdowhn-process is finished, then powers off esxi
# completely
##########################################################################################################################
VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | cut -d "\"" -f2`
echo "Powered on: $name"
echo "Shutting Down: $name"
vim-cmd vmsvc/power.shutdown $VM > /dev/null &
fi
done
while true ; do
RUNNING=0
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
echo "Waiting..."
RUNNING=1
fi
done
if [ $RUNNING -eq 0 ] ; then
echo "Gone..."
break
fi
sleep 1
done
echo "Now we shutdown the host..."
/sbin/shutdown.sh && /sbin/poweroff
******************************************************