How to Create Cron Jobs and Schedule Tasks on Raspberry Pi OS

Jinku Hu Feb 02, 2024
  1. Cron Jobs and Basics of Task Scheduling on Raspberry Pi OS
  2. Add Scheduled Commands in Crontab Configuration File on Raspberry Pi OS
  3. Use Cron to Schedule System Shutdown on Raspberry Pi OS
How to Create Cron Jobs and Schedule Tasks on Raspberry Pi OS

This article will introduce how to create scheduled tasks using cron jobs on Raspberry Pi OS.

Cron Jobs and Basics of Task Scheduling on Raspberry Pi OS

Raspberry Pi OS provides a preinstalled cron daemon to execute scheduled commands like most contemporary Linux distributions.

Daemons are programs created at the operating system startup. They mostly run until the system shuts down or an explicit user delivers a signal to stop them.

Usually, several common daemon processes are running by default on Raspberry Pi OS. You can often identify them using the d suffix in the name of the process, which is a somewhat accepted convention on Unix-based systems.

The cron daemon lets you automate the execution of commands at specific times or intervals. Since the operating system administration involves many repetitive tasks, the latter feature is highly desirable.

Commands scheduled and executed by the cron daemon are called cron jobs. Generally, cron daemon reads the configuration file crontab, which contains scheduled commands and corresponding times using a special syntax.

Since the Raspberry Pi OS is a multi-user system, the cron daemon can execute user-specific scheduled tasks, which will be stored in corresponding crontab files.

Before you start adding commands to your crontab file, it’s good to verify that the system runs cron daemon using the following command.

sudo systemctl status cron.service

If the daemon is inactive for some reason, you can run the next command to start it and enable it to be on the safe side.

sudo systemctl start cron.service && sudo systemct enable cron.service

Add Scheduled Commands in Crontab Configuration File on Raspberry Pi OS

Individual user files are stored in the /var/spool/cron/crontabs directory, where configuration files have the name of the corresponding user. You can start editing your crontab with the command.

crontab -e

This command might ask you to choose one of the command line editors for editing the crontab file. You can select any option that you are most comfortable with.

Once the crontab file is opened, it usually includes commented-out text which describes configuration file specifics and even provides a simple cron job entry. In this case, we will start writing our sample commands after the commented lines in the configuration file.

Each cron job (task) is defined in a single line which starts with the five time-date fields and is followed by the command, while each of them is delimited by whitespaces.

The time-date fields specify when to run the given command and have the following order.

minute hour dom month dow command

Each time-date field can have a single integral value, a star (*) character, dash-separated values for specifying ranges, or a comma-separated list of integers/ranges.

Also, a range notation can be followed by the slash and the integer to write a series of numbers more concisely. Meanwhile, the next table shows valid numbers for each time-date field and corresponding meaning.

Field Description Range
minute n-th minute of hour 0 - 59
hour n-th hour of day 0 - 23
dom day of month 1 - 31
month month of year 1 - 12
dow day of week 0 - 6 (0 means Sunday)

Use Cron to Schedule System Shutdown on Raspberry Pi OS

We will demonstrate multiple time-date formats to schedule a shutdown command on Raspberry Pi OS in the following examples. The next cron job line will schedule a system shutdown every weekday at 1:00 am.

 0 1 * * 0-6 sudo shutdown -h

Note that the shutdown command requires root privileges if you’re adding it from the non-root account. Additionally crontab -e command needs to be executed using the sudo prefix.

Once you add a cron job using the crontab command, it should take effect immediately, so you can test commands by scheduling them one or two minutes ahead.

Alternatively, you can schedule the same cron job on odd days of the month at 1:00 am using the next command.

 0 1 1-31/2 * * sudo shutdown -h

You can find additional materials on more advanced scenarios of using cron jobs in the Debian handbook or Arch Linux wiki.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - Raspberry Pi

Related Article - Raspberry Pi OS