View unanswered posts | View active topics It is currently Thu Oct 17, 2019 10:20 am

Reply to topic  [ 5 posts ] 
 How-To: shutdown ESXi Free via an USB UPS 
Author Message

Joined: Fri Jul 23, 2010 12:08 am
Posts: 39
Location: Prato, Italy
Reply with quote
Post How-To: shutdown ESXi Free via an USB UPS
Author: Spike
Link to my blog post


The purpose of this How-To is to let an USB UPS (in my case an APC Back-UPS 800VA) communicate to ESXi to shutdown or suspend VMs and then shutdown host in case of power failure.
The source script's author is helux where if you can find the original thread and the original script in the sources links at the bottom of this article.
I have customized the script with a logging system and with an option to decide if to suspend or shutdown the virtual machines.

The problem

Due to the fact that, since VMWare ESXi 3.5U4, read and write access from RCLI (Remote Command Line Interface) has been disabled, we are unable to use scripts like written by lamw (William Lam).
Our only possibility is to install the USB UPS monitoring tool in a Virtual Machine, then communicate to ESXi via SSH and then run a script copied on the ESXi itself.

In my case I have an Ubuntu Server 10.10 installed as a Virtual Machine. Here I have installed apcupsd software to monitor the UPS.

Create ssh keys

I have generated an SSH key for root to let the VM communicate with ESXi.

sudo ssh-keygen

Do not set a passphrase for the private key otherwise we can't automate the process.
After having enable the SSH server in ESXi, transfer the public key of the VM to ESXi

sudo ssh-copy-id -i /root/.ssh/ root@

Change the path to the public key file and the IP address of the ESXi.

The script

Download the script with wget in your linux VM or copy and paste the content in a new document


Edit the script and, at the beginning, customize it on your needs, such as enable logging system, set the log file, decide what VMs should do (suspend, shutdown) etc.

# Set here if you want to suspend or shutdown the Virtual Machines
# Options available: suspend ; shutdown

# To enable logging, set the following variable to 1

Then transfer powerdown_esxi4. to ESXi

scp root@

Please note that if you transfer a text/script file from Windows via vSphere Client to a datastore, a line feed "^M" will be added so the file will be unusable.
If you want you can wget it.
Now login into your ESXi server and make executable

chmod +x /bin/

Change UPS monitor software behaviour

We have to tell our UPS monitor software that during the Shutdown Sequence he has to run our script.

Network UPS Tools

Edit /etc/nut/upsmon.conf, change the line

SHUTDOWNCMD "/sbin/shutdown -h +0"

and call a custom script instead

SHUTDOWNCMD "/usr/local/bin/"

Create the script /usr/local/bin/

ssh root@ ''

The problem with Network UPS Tools and my APC UPS

I encountered 2 problems using my APC UPS:
1) NUT can't power on my APC after power outage
2) My APC has ups.delay.shutdown= 20
That means that the amount of time the UPS will wait before shutting down is only 20s. I saw that I need at least 1:10 min, so I had to change it to 90s.
I did it with

sudo upsrw -s ups.delay.shutdown=90 -u local_mon -p mypass apc@localhost

but after a service restart, the UPS loses this setting.


Edit /etc/apcupsd/apccontrol this way

echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
ssh root@ ''
${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown"

The problem with apcupsd and my APC UPS

As stated in 2 thread in VMWare Communities:
ESXi-4.1: USB passthrough problem with APC Back-UPS CS 650
apcaccess: status output minimal when using USB device passthru on ESX

There is a problem in the USB passthrough of APC UPSes: apcupsd doesn't recognize many parameters.
However essential parameters like LOADPCT (Percent Load Capacity), BCHARGE (battery charge), TIMELEFT, MBATTCHG (Minimum Battery Charge), MINTIMEL (Minimum Time Left) are properly recognized so we can continue using our script.
At the moment it is the best solution I have found.

Compress everything and generate oem.tgz

To load the ssh key and the script at system boot, you have to add it to /bootbank/oem.tgz, as ESXi will clear everything that doesn't belong to the system itself.
If you don't have any oem.tgz, create it with

tar -cvzf /bootbank/oem.tgz .ssh/ /bin/

If you have already an oem.tgz for something else, extract the content to a folder, as the tar installed in ESXi doesn't support appending of files,

mkdir oem
tar -xvzf oem.tgz -C oem/

copy the script preserving the directory structure of /bin

mkdir /bootbank/oem/bin/
cp /bin/ /bootbank/oem/bin/

copy also the ssh key

cp -R /.ssh/ /bootbank/oem/

compress again the archive

cd /bootbank/oem/
tar -cvzf /bootbank/oem.tgz `ls -A`

Enjoy :)

VMWare /etc - ESXi U4 Ends Free Version Read and Write Access from the RCLI

VMWare Communities: esxi shutdown script for ups monitoring by helux

VMWare Communities: - Spegnere ESXi tramite un UPS APC

Thu Apr 07, 2011 6:36 am

Joined: Sat Apr 23, 2011 10:02 pm
Posts: 1
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
I picked up a APC UPS today in the hopes of finding something like this. Tomorrow I'll see if i can get it to work.


Sat Apr 23, 2011 10:07 pm

Joined: Thu Feb 25, 2010 1:08 am
Posts: 12
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
You can get something native here : ... ware-esxi/

Wed May 18, 2011 5:37 am

Joined: Fri Jul 23, 2010 12:08 am
Posts: 39
Location: Prato, Italy
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
Thanks for the recommendation!
I already saw that post and I think what it is supposed to do, because I don't know very well french!
I think that the problem between NUT and my APC UPS will persist but I will give it a try!

Wed May 18, 2011 6:11 am

Joined: Sat Dec 08, 2012 11:46 pm
Posts: 2
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS

Im having a major issue trying to get /bootbank/oem.tgz to unpack the /bin/ script. Its driving me crazy.

When i reboot the machine the file is gone. If i unpack the oem.tgz file, the file appears in /bin/ and will run (shutting down the VM's and then host fine)

I wonder if the issue is due to the way the file is being tar'd, as i get this error about absolute paths


That said, if it was tar wrong, then why is is unpacking fine and then able to execute? Please any help would be HIGHLY appreciated!!!!

EDIT: ok, found the issue. I didn't realise the changes that came in ESX5.0

| The oem.tgz needs to be placed at /bootbank as well as /altbootbank
| "--- oem.tgz" needs to be added to both boot.cfg files at /altbootbank and /bootbank.

Sorted, my scripts are persistent!!

Sat Dec 08, 2012 11:54 pm
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.