2

We had a virtual machine running on a VMWare Server host on Windows Server 2003.

The machine is set up with non persistent disks.

We had a windows task schedule set up that ran a batch file to reset the machine each week so that it returned to it's original state.

The batch file that we had running was:

"C:\Program Files\VMware\VMware Server\vmware-cmd" "C:\Virtual Machines\VirtualMachineName\VirtualMachineName.vmx" stop hard
"C:\Program Files\VMware\VMware Server\vmware-cmd" "C:\Virtual Machines\VirtualMachineName\VirtualMachineName1.vmx" start

We have since migrated this machine to the free version of ESXi 4.1.

Can anyone let me know if and how it is possible to schedule such a restart?

4
  • enabled ssh access to your ESXi
  • add cron job to the root crontab(/var/spool/cron/crontabs/root)

You can start, reboot, stop, and suspend virtual machines using vmware-cmd:

vmware-cmd -H <vc_system> -U <user> -P <password> --vihost <esx_host>
/vmfs/volumes/Storage2/testvm/testvm.vmx stop soft
vmware-cmd -H <vc_system> -U <user> -P <password> --vihost <esx_host>
/vmfs/volumes/Storage2/testvm/testvm.vmx start soft
  • But /var/spool/cron/crontabs/root isn't persistent following reboots. – Chopper3 Feb 25 '11 at 15:23
  • Then add cron rules from "/etc/rc.local"(use echo "..." > /var/spool/cron/crontabs/root) – ooshro Feb 25 '11 at 15:54
  • Nope /etc/rc.local isn't persistent either. – Chopper3 Feb 25 '11 at 19:09
  • @Chopper3♦: /etc/rc.local is persistent on ESX and ESXi 4.1 systems. – user9517 supports GoFundMonica Feb 26 '11 at 17:32
2

If you haven't already done it you will need to enable ssh access on your ESXi host.

Once you have done that you can then put vmware-cmd commands in a script and run them from cron e.g.

#!/bin/bash
/usr/bin/vmware-cmd "Centos 5" stop hard
sleep 30
/usr/bin/vmware-cmd "Centos 5" start

would stop and then start the machine "Centos 5" on the current host.

Put this in the root crontab

30 1 * * 0 /path/to/restart-script

and you should be good to go.

Edit:

As chopper3 notes the crontab is not persistent across reboots so you will need to edit /etc/local file and add commands to recreate the root crontab e.g.

/bin/kill $(cat /var/run/crond.pid)
/bin/echo "30 1 * * 0 /path/to/restart-script" >>/var/spool/cron/crontabs/root
/bin/busybox crond
0

Could you also perhaps schedule a reboot within the virtual machine itself? Whether it is a Linux or a Windows VM you should be able to use cron or Task Scheduler inside the guest to perform the scheduled reboot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.