On VMware ESX/ESXi, changes to the root crontab are lost when the host reboots. Here’s how to add a cron job that survives reboots by re-creating it from /etc/rc.local.
Enable SSH on the host first, then log in as root.
1. Add the Job to the Root Crontab
- Edit
/var/spool/cron/crontabs/root. - Add your job on a single line, for example:
5 0 * * * /full/path/to/script arguments/with/full/path > /full/path/to/logfile 2>&1
- The file is read-only, so save and quit vi with
Escthen:wq!.
5 0 * * * means 5 minutes past midnight every day. See the crontab(5) man page for the full schedule syntax.
2. Re-create the Job at Boot via /etc/rc.local
Edit /etc/rc.local (for example, vi /etc/rc.local) and add these three lines at the end. The first stops crond, the second appends your job to the root crontab, and the third starts crond again:
/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
Save and exit, then run the command below so your /etc/rc.local change itself survives a reboot:
auto-backup.sh
Remember: every time you change the cron job, update /etc/rc.local too and run auto-backup.sh again.
