How to schedule a task in Linux

Sometimes, you need a task to run at a particular time on a particular schedule. For Project Spoofy, I need the Raspberry Pi to reboot periodically so that any new photos added to ownCloud will be subsequently added to the fbi slideshow. For simplicity, I’m going to schedule a task in Linux to reboot the Pi each night at about 2:30AM, so no one should be affected by the short break in the slideshow.

Schedule a task in Linux

To schedule a task in Linux, use the cron utility to define the schedule as well as the command to be executed. Cron has to be edited by the root account, so a standard user (pi) would need to execute the following command:

cron -e -u root

If logged in as root already, simply execute cron -e

Debian will ask which utility to use to edit the crontab file; I prefer using nano as with everything else. Once the crontab file is open in nano, you will see some information as well as some example formatting. Basically, every task should exist on a separate line starting with the scheduling:
┌───────────── min (0 – 59)
│ ┌────────────── hour (0 – 23)
│ │ ┌─────────────── day of month (1 – 31)
│ │ │ ┌──────────────── month (1 – 12)
│ │ │ │ ┌───────────────── day of week (0 – 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ │ │
│ │ │ │ │
* * * * * command to execute
To reboot at 2:30AM every day, the following line must be appended to the end of the file:

30 2 * * * /sbin/shutdown -r now

The biggest thing to remember is ensuring that the time and time zone are correct. As a pilot, I understand the importance of UTC (or Zulu) time for coordinating across time zones, so I prefer to keep my remote machines set to UTC and do the math accordingly!

One thought on “How to schedule a task in Linux”

Leave a Reply