Crontab on Raspberry Pi

Crontab is used for configuring scheduled tasks on Raspberry Pi. In short, cron is the name of the tool, crontab is the cron table listing the jobs that cron will be executing while these socalled jobs are cronjobs.

How to edit crontab on Raspberry Pi

Run crontab (cron table) with the -e flag to edit the cron table:

crontab -e

Crontab commands

crontab -e Edit crontab file (alternatively, create a crontab file if it does not exist).
crontab -l list all crontab jobs.
crontab -r Remove crontab file.

Schedule cronjobs on Raspberry Pi

Cronjobs are created using the following components: minute, hour, day of month, month of year, day of week, as well as the command to be executed.

Graphical Overview

m h dom mon dow   command
* * * * *  command to execute
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

For example, to execute a command on the 1st of January, June & December at 00:30 – the following will need to be typed in:

min    hour     day of month     month      day of week
30     0        1                1,6,12     *	

Cheatsheet

@hourly - Shorthand for 0 * * * *
@daily - Shorthand for 0 0 * * *
@weekly - Shorthand for 0 0 * * 0
@monthly - Shorthand for 0 0 1 * *
@yearly - Shorthand for 0 0 1 1 *
Crontab Format:
MIN HOUR DOM MON DOW CMD
Format Meanings and Allowed Value:
MIN     Minute field    0 to 59
HOUR    Hour field      0 to 23
DOM     Day of Month    1-31
MON     Month field     1-12
DOW     Day Of Week     0-6
CMD     Command     Any command to be executed.

Examples of Using Cron on Raspberry Pi

How to add apt-update and apt-upgrade as a cronjob on Raspberry Pi

sudo crontab -e
0 3 * * * root /usr/bin/apt-get update && /usr/bin/apt-get upgrade -q -y

How to run script on Raspberry Pi with cronjob

sudo crontab -e
0 0 * * *  /home/pi/myscript.sh

How to run a cronjob on Raspberry Pi on startup or restart

sudo crontab -e
@reboot  /path/to/job
@reboot  /path/to/myscript.sh
@reboot  /path/to/command argX argY

How to run a cronjob on Raspberry Pi on startup with delay

sudo crontab -e
@reboot sleep 300 && /home/pi/myscript.sh

2 thoughts on “Crontab on Raspberry Pi”

  1. Thank you for your post.

    I installed Nextcloud 23 via docker on a raspberry pi 4 and tried different ways to execute nextcloud’s cron.php file.

    crontab -e
    */1 * * * * docker exec -u www-data -it {name or id of container} php cron.php
    –> works perfect if I execute it manually

    crontab -u www-data -e
    */1 * * * * docker exec -u www-data -it {name or id of container} php cron.php
    –> Error: Owner id of config.php: XX

    crontab -u pi -e
    */1 * * * * docker exec -u pi -it {name or id of container} php cron.php
    –> Error: Owner id of config.php: XX

    do you have any idea how to fix it?

    1. Hi Taco,
      Did you fix it? Looks like owner issues.
      I believe the best course of action would to SSH into your Pi 4 and create:

      crontab -e
      */5 * * * * php -f /var/www/nextcloud/cron.php

      Note: Replace path if different!

      Verify the cron job has been added:
      crontab -u www-data -l

      Source: Nextcloud Background Jobs

Leave a Comment

Your email address will not be published.