December 11, 2018

266 words 2 mins read

Adding a Cron Job to ESXi

Adding a Cron Job to ESXi

There is often a need to have scheduled maintenance tasks run on the free ESXi edition which unfortunately has some crippled features. An example of this is when implementing the fine ghettoVCB backup scripts.

As this is an often needed feature I thought I’d document the process in this article.

Having enabled ssh access to your ESXi server, ssh in as root.

Firstly, add the cron job to the root crontab:

  1. Edit /var/spool/cron/crontabs/root.
  2. Add the line (all on one line) 5 0 * * * /full/path/to/script arguments/with/full/path > /full/path/to/logfile 2>&1.
  3. Run the command cat /var/run/crond.pid.
  4. That will print the process number of the running crond, such as 12345.
  5. Run the command kill 12345 where “12345” should be replaced with the number output by the previous command.

For details of the meaning of 5 0 * * * (5 minutes past midnight every day) read the man page on any Unix/Linux server.

Configure to Survive Reboots

Now, add a command to /etc/rc.local to re-generate the cron job when ESXi reboots.

Edit /etc/rc.local.

At the end of the file add the following three lines:

/bin/kill $(cat /var/run/crond.pid)
/bin/echo '5 0 * * * /full/path/to/script arguments/with/full/path > /full/path/to/logfile 2>&1' >> /var/spool/cron/crontabs/root
/bin/busybox crond

The first kills the running crond, the second adds the new cron job to the root crontab file, while the third restarts crond.

Save and exit the editor.

Run the command auto-backup.sh so that the change to /etc/rc.local survives a reboot.

Reminder

Every time you change the cron job, remember to update /etc/rc.local as well and run the auto-backup.sh command to store the new /etc/rc.local file.

comments powered by Disqus