ESXi – Add cron Job
ESXi cronjob, esxiThis explains how to add a cron job to ESXi host. And later make it reboot persistent.
Firstly, add the cron job to the root crontab:
Edit /var/spool/cron/crontabs/root
# vi /var/spool/cron/crontabs/root
Add the line (all on one line)
5 0 * * * /full/path/to/script arguments/with/full/path > /full/path/to/logfile 2>&1
Run the following command to detect the process number under which crond is running.
# cat /var/run/crond.pid
Once we know the process number of the running crond, such as 12345. Run following command:
# kill -HUP 1234
“12345” should be replaced with the number output by the previous command
In order to restart the crond process use this command:
# /usr/lib/vmware/busybox/bin/busybox crond
This allow us now to test the correct working of the cronjob. Now we must make it persistent.
Now, add a command to /etc/rc.local.d/local.sh to re-generate the cron job when ESX/ESXi reboots
Edit /etc/rc.local.d/local.sh, using a command such as
# vi /etc/rc.local.d/local.sh
At the end of the file, add 3 lines (using “G” then “O” in vi).
The first kills crond, the second adds the new cron job to the root crontab file, ad the third restarts crond:
/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
/usr/lib/vmware/busybox/bin/busybox crond
Save and exit the editor (Press the “Esc” key then “:wq” then press “Return” in vi)
Run the command “auto-backup.sh” so that the change to /etc/rc.local survives a reboot.
Every time you change the cron job, remember to update /etc/rc.local as well and run the “auto-backup.sh” command to backup the new /etc/rc.local file.
# autobackup.sh
Warning:
As you can see in the local.sh file, this script will not be executed if the host has Secure Boot enable
References:
Gathering esxtop performance data using cronjobs
ESXi – Add cron Job