VMWare Server stop and start via script
From Noah.org
The "vmware-cmd stop" command does not work. For example, when I run the following:
vmware-cmd /var/lib/vmware/Virtual\ Machines/example/Ubuntu.vmx stop
I always get this error:
VMControl error -8: Invalid operation for virtual machine's current state: Make sure the VMware Server Tools are running
I don't know what that means and I didn't find an answer on Google.
So when I want to hard restart a server from a script I end up doing something like this:
BAD_PID=`ps -ef | grep example/Ubuntu.vmx | grep -v grep | awk '{print $2}'`
kill -INT $BAD_PID
sleep 10
kill -9 $BAD_PID
vmware-cmd /var/lib/vmware/Virtual\ Machines/example/Ubuntu.vmx start
if [ $? -ne 0 ]; then
echo "Example server did not restart."
fi
VMWare itself is pretty reliable, but some of my web servers are not. I have a watchdog script similar to the one below that I run from cron. In the real world you might not want to reboot your web server just because it fails to respond to a single query.
#!/bin/sh
# This checks that example server is responding to queries.
# If it fails then it sends an email alert to a bunch of people.
DTS=`date`
wget -q --no-cookies -O - http://www.example.com/index.php?q=94105 | grep -q "San Francisco"
if [ $? -ne 0 ]; then
mail 4155551212@vtext.com noah@example.com -s "ALERT! www.example.com is not responding." <<HERE_ALERT
${DTS}
www.example.com did not return expected response to test query.
Example server may be down.
Trying to hard restart it...
HERE_ALERT
BAD_PID=`ps -ef | grep example/Ubuntu.vmx | grep -v grep | awk '{print $2}'`
kill -INT $BAD_PID
sleep 10
kill -9 $BAD_PID
vmware-cmd /var/lib/vmware/Virtual\ Machines/example/Ubuntu.vmx start
if [ $? -ne 0 ]; then
mail 4155551212@vtext.com noah@example -s "ALERT! Example server did not restart." <<HERE_RESTART
${DTS}
Example server did not restart.
VMWare may be down.
HERE_RESTART
fi
fi