Making cron job / entries persistent across reboot on ESXi host
search cancel

Making cron job / entries persistent across reboot on ESXi host

book

Article ID: 375310

calendar_today

Updated On:

Products

VMware vSphere ESXi 7.0 VMware vSphere ESXi 8.0

Issue/Introduction

ESXi uses the cron to schedule several internal housekeeping routines. In some instances, new cron jobs may need to be added to the ESXi host. However, they won't be persistent after rebooting by default.

Environment

vSphere ESXi

Cause

Cron jobs on ESXi hosts do not persist across reboots without manual edits of the local.sh script file.

Resolution

1. Backup the Existing Crontab File

First, make a backup of the current root crontab file:

cp /var/spool/cron/crontabs/root /var/spool/cron/crontabs/root.old

2. Edit the Root Crontab

Add the desired cron job to the root crontab:

vi /var/spool/cron/crontabs/root

In the editor, add your cron job with the required schedule and command.

3. Edit the /etc/rc.local.d/local.sh File

To ensure the cron job is re-created upon reboot, follow these steps:

vi /etc/rc.local.d/local.sh

At the end of the file, add the following 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 /usr/lib/vmware/busybox/bin/busybox crond

4. Save and Exit the Editor

Press the following keys to save and exit:

    • Press Esc to exit insert mode.
    • Type :wq and press Return to save and quit the editor.

5. Run the auto-backup.sh Command

Run this command to ensure that the changes to /etc/rc.local are persistent across reboots:

auto-backup.sh

Note:

Every time the cron job is modified, ensure that /etc/rc.local.d/local.sh is updated and run the auto-backup.sh command to back up the changes.

Additional Information

Cleaning up & restoring:

  1. Edit /var/spool/cron/crontabs/root & remove cron job entry
  2. Restart crond using busybox
  3. Revert to original local.sh file
  4. cp /scratch/downloads/local.sh.original /etc/rc.local.d/local.sh
  5. Verify the above step executed successfully by grepping from any "cron" entry.
  6. Delete local.sh.<TimeStamp> if any
  7. Run auto-backup.sh