VMware Cloud Community
airswimmmer
Contributor
Contributor

remotely shutdown ESX with a delay

Hi all,

I am trying to gracefully shutdown all the VMs and ESX remotely using a script which can be called by APC Powershute when power loss. I have overcome many problems but the last one I am really not sure what to do.

I don't have problem with VM shutdown, but for host shutdown, I use plink to send the command "shutdown -h 5" to shutdown the host in 5 mins, this gives enough time to shutdown the VMs. the problem is that:

  • I can only call one script, so the commands in the scrip has to shutdown both hosts

  • the commands will be excuted line by line and it has to wait for previous command to finish (either successful or fail) before executing the next line of command.

  • the "shutdown -h 5" command does NOT finish until 5 mintus is up, so plink will be stuck there and wait for the 5 minutes before sending the same command to second host

how do I move on to the next host shutdown command without waiting for the first one to finish on the first host?

Thanks

Reply
0 Kudos
10 Replies
lamw
Community Manager
Community Manager

Have you taken a look at this script: , it allows you to specify a list in which ESX(i) will be powered off and you'll just want to specify the last ESX(i) hosting the VM to shutdown last.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

If you find this information useful, please award points for "correct" or "helpful".

athlon_crazy
Virtuoso
Virtuoso

How if you configure your first VM (on esx1) to initiate shutdown for esx2 b4 your 1st VM going to shutdown.

vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org
Reply
0 Kudos
lamw
Community Manager
Community Manager

The script allows you to specify a list of hosts to power off in a specific order. You'll want to have your last ESX(i) system to host the VM that is initiating the power off.

(e.g.)

If you have ESX1,2,3 and say you want to power off in the following order (ESX2,ESX3,ESX1) then you'll want the VM kicking off the shutdown to live on ESX1. It'll then specify ESX2 to shutdown all VM(s) and then itself, go to ESX3 and do the same and then finally ESX1 shutdown all VMs, except for the VM that is initiating it . Once all VMs have been powered off on ESX3, it does a force shutdown of ESX3 while the VM that is running the script is still on.

It's pretty straight forward, so I'm not sure where the confusion lies.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
athlon_crazy
Virtuoso
Virtuoso

Dude, another option :

Put "&" sign at the end of your 1st "shudown -h 5" command. You can double check this with simple script as below :

#!/bin/bash

sleep 6 &

cat /etc/hosts

ps ax | grep sleep

So command with "&" should running at the background, then proceed with the remaining command without need the first command to be finished first.

vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org
Reply
0 Kudos
airswimmmer
Contributor
Contributor

I thought about that, but not sure how to send commands from one host to another?

Reply
0 Kudos
airswimmmer
Contributor
Contributor

I tried that, it works fine from the console, but from a bat file in windows, it doesn't work, I tested by making the following bat file:

===

plink -ssh root@10.10.1.21 -l root -pw Password1 "shutdown -h 5" &

md d:\test

===

the second command still doesn't execute until the first one is finished or cancelled.

Reply
0 Kudos
athlon_crazy
Virtuoso
Virtuoso

Try without quote " "

plink -ssh root@10.10.1.21 -l root -pw Password1 shutdown -h 5 &

vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org
Reply
0 Kudos
airswimmmer
Contributor
Contributor

Thanks for your reply, I thought you always need to quote it if you send the commands directly in line with plink script, but it didn't make a difference, once the first line is executed, it will pause there until time is up or canceled.

I have also tried creating the script file on the ESX console, and use plink to call the script, it's the same problem.

Reply
0 Kudos
athlon_crazy
Virtuoso
Virtuoso

Okay, nvm, as I told earlier, your can let's your 1st VM (esx1) to remote shutdown your esx2. I know this can be done on any Linux distro but donno on windows. So, everytime your linux VM try to shutdown, normally it's will run few scripts (ie. stop running services). Call / put your remote_shutdown script under this "init or runlevel 6".

p/s : don't forget, If you mistakenly down this VM, your esx2 also go down with all running VMs ya..

vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org
airswimmmer
Contributor
Contributor

Hi all,

Thanks for all your help, I have got a workaround which is probably a better way to do it. I have been trying to somehow force to move on to the next line of command after the "shutdown -h 5" command is sent, now instead of waiting for 5 minutes in the shutdown command, I moved the 5 mintus wait to be in windows script using the "timeout" command in WIndows 2003. so the script now looks like this:

=====

<use plink.exe to send commands to shutdown the VMs on esx1>

<use plink.exe to send commands to shutdown the VMs on esx2>

timeout -t 5

<use plink to send shutdown now cmd to esx1>

<use plink to send shutdown now cmd to esx2>

======

thanks for all your help anyway.

Reply
0 Kudos