Stupid Linux Tricks: cron task @reboot

Posted by Timothy O'Connell in Code on October 15, 2008

A little known fact of cron is that you can use it to execute commands as soon as your machine returns from a reboot. If, for example, I create a file in /etc/cron.d/ called "whatever" that looks like this:

@reboot    root    `which sshfs` user@remotemachine:/mnt/data  /mnt/data

The above command will be executed once the system is fully booted. This is ideal for cases in which depend on a number of services or in which manual intervention is required. One could, for example, create a file called "whatever" in /etc/cron.d/ to remind him that he needed to manually intervene once the boot process was finished and that he was now free to do just that:

@reboot    root    echo "Don't forget the sshfs mount!" | mail -s "[`hostname`] Reboot!" root

There are a handful of other, similarly useful shortcuts that Vixie built into cron but that aren't particularly well known:

    @yearly        Run once a year, "0 0 1 1 *".
    @annually      (same as @yearly)
    @monthly       Run once a month, "0 0 1 * *".
    @weekly        Run once a week, "0 0 * * 0".
    @daily         Run once a day, "0 0 * * *".
    @midnight      (same as @daily)
    @hourly        Run once an hour, "0 * * * *".

(Excerpted from the man page)

1 Comment

 Tim

Handy tips – I never knew these existed. The @reboot one will come in handy for me when I’m on a system that doesn’t use the RedHat style /etc/rc.local script to run commands on boot.

Leave a comment

WP_Big_City