Click to See Complete Forum and Search --> : Need VMware Start Script


klackenfus
04-01-2008, 06:53 PM
I had an inconvenient experience today. I have two virtual machines running on Open Suse 10.2, that developers are testing a product against. One is a Windows Server 2000 image and the other is CentOS 5. Not that the guest OS really matters. Somehow, I managed to remotely shutdown VMware today. Oh and yes, I was accessing both VMs through through the Windows VM and from there, the CentOS VM through Putty. Did I mention I was 63 miles away from the VMware server? Yes, I was. I have two basic scripts to start the VMs:

vmrun start /virtual2/ts2000/wts4.vmx

vmrun start /virtual2/rhel/rhel4.vmx

What I want to do and I'm not enough of a coder to do, is wrap some script around both of these to test the running state of both VMs and then, if they are not running, to start them. Ideally, all I really need is the Windows VM, then I can Putty into CentOS and start that VM. I would then create a Cronjob to run the script like every 15 minutes.

Any thoughts?

Thanks!

klackenfus
04-01-2008, 08:14 PM
I need to clarify:

"then I can Putty into CentOS and start that VM. I would then create a Cronjob to run the script like every 15 minutes."

If I can RDP into the Windows VM, I can SSH into the Suse box and start the CentOS VM.

gamblor01
04-02-2008, 10:57 AM
I don't have any VMware installed on a Linux box (I have VMware Fusion on my Mac) so I suppose that it would be good to know the name of the process. Basically, to get started I would ensure both of the machines are running and get the output of "ps -ef" on the system.

Once you have the output you can know what to grep for, and store this into a variable in a shell script. For example:

VMSRUNNING=$(ps -ef | grep 'vmware')

Then we can do some testing on the variable and decide what to do from there. I'm sure someone here is willing to help with the scripting part tough. :)

klackenfus
04-02-2008, 11:12 AM
Actually, shouldn't we be grepping on vmx, since vmx is actually the "executable" for the running VMs? In the case of my host server, the output of that is:

ps -ef|grep vmx
emaude 4439 1 52 Apr01 ? 09:38:20 /usr/lib/vmware/bin/vmware-vmx -C /virtual2/ts2000/wts4.vmx -@ ""
emaude 6348 1 7 Apr01 ? 01:14:52 /usr/lib/vmware/bin/vmware-vmx -C /virtual2/rhel/rhel4.vmx -@ ""
root 18739 18720 0 10:10 pts/1 00:00:00 grep vmx
localhost:~ #

bwkaz
04-02-2008, 06:44 PM
ps -ef | grep [v]mx | wc -l

should give you the number of vmx processes. The square brackets are there to exclude the grep process. Run this in a backquote (or $(...)-type) expression to assign it to a variable, then test the variable. :)

gamblor01
04-02-2008, 08:55 PM
Actually, shouldn't we be grepping on vmx, since vmx is actually the "executable" for the running VMs?
Sure...grep for vmx then. I should have been more explicit, but I didn't actually mean for the exact term you use to be the string "vmware" ... I meant vmware to be generic for some string that represents the vmware processes.

So now you can probably just write a shell script that does something like this (sorry I don't have the time for a full script right now with error conditions and what not...about to go meet some people for drinks):


#!/bin/bash

# check if Windows VM is running
VMWIN=$(ps -ef | grep 'wts4.vmx')

# if string is empty then the VM is not running; start it
if [ -z "$VMWIN" ]; then
vmrun start /virtual2/ts2000/wts4.vmx
fi

# check if Linux VM is running
VMLINUX=$(ps -ef | grep 'rhel4.vmx')

# if string is empty then the VM is not running; start it
if [ -z "$VMLINUX" ]; then
vmrun start /virtual2/rhel/rhel4.vmx
fi



For now you might try replacing the vmrun start commands with echo commands just to tell you whether or not the systems are up/down. Once you're convinced the logic works properly then you can get more dangerous.

Another thing is, what happens if the vmrun start command is issued when the VM is already running? Does it do anything or just exit? Might be useful information for you to know.

klackenfus
04-03-2008, 01:23 PM
Very cool! I will try it tonight. Thanks!

klackenfus
04-03-2008, 08:52 PM
From the script, I'm getting this:

./vmware_start.sh
./vmware_start.sh: line 12: syntax error near unexpected token `fi'
./vmware_start.sh: line 12: `fi'

gamblor01
04-04-2008, 12:41 AM
Hmm...well that shouldn't be happening. Line 12 is not "fi" in my file so that means you added something. You might have forgotten a closing quote/brace or something like this. I made a few slight changes so try this exactly:


#!/bin/bash

# check if Windows VM is running
VMWIN=$(ps -ef | grep 'wts4.vmx' | grep -v 'grep')

# if string is empty then the VM is not running; start it
if [ -z "$VMWIN" ]; then
echo "Starting Windows VM..."
# vmrun start /virtual2/ts2000/wts4.vmx
else
echo "The Windows VM is already running"
fi

# check if Linux VM is running
VMLINUX=$(ps -ef | grep 'rhel4.vmx' | grep -v 'grep')

# if string is empty then the VM is not running; start it
if [ -z "$VMLINUX" ]; then
echo "Starting Linux VM..."
# vmrun start /virtual2/rhel/rhel4.vmx
else
echo "The Linux VM is already running"
fi

Here are the three changes:

1. I added in the echo commands for you and commented out the vmrun commands. This should allow you to test it as is and all it will do is echo some statements to the console. It won't actually start anything until you uncomment the 2 lines for the vmrun start commands.

2. I added in an else clause which will output that the VM is already started if it's running.

3. I had the grep command remove grep from the output (which is why bwkaz suggested [v]mx). Basically, sometimes when you grep for something, the system finds that grep command as a running process and outputs it like this:


$ ps -ef | grep 'vmx'
501 220 203 0 0:00.00 ttys000 0:00.00 grep vmx

This is an undesirable occurrence because the script could receive a similar string like that as the output of the ps -ef command, and it would assume that since the string was non-empty that the VM must already be started. This is clearly not the case so I added in the extra pipe to "grep -v 'grep" which says to suppress any output that has the word "grep" in it. So basically what it does now is to get the ps -ef output, filters it to only show lines with the .vmx value in it, and then filters it again by removing any line with the word "grep" in it. I hope that makes sense...if you're already familiar with grep please don't take it personally. I'm just explaining in advance in case you don't know. ;)

In any case I just ran that script and this was my output:

$ ./vmware_start.sh
Starting Windows VM...
Starting Linux VM...

Obviously since I don't have those virtual machines on my system it thinks that it needs to start them both...so it appears to be working. You should be able to copy that to a file and run it just like I did. If you have any problems let me know.