banner scheduled tasks on pi -- Erik Mclean / Unsplash / ThomasDyan / RaspberryTips

How to Schedule a Task on a Raspberry Pi?

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

Scheduling commands or scripts on a Raspberry Pi, and on Linux generally, is not transparent for a beginner. There are many tips you should know in order to make it work every time, and in this article, I’ll give you recommendations in detail.

To schedule a task on Raspberry Pi, use the “crontab” tool. It’s useful to run scripts at specific times or on boot.

What is cron, exactly? I’ll give a brief explanation, show you some examples of how to use it, and some tips and tricks at the end.

If you’re new to Raspberry Pi or Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your Raspberry Pi. Click here to get it for free!

What Is crontab?

When we talk about scheduling tasks, people often get confused by cron vs. crontab.
Let’s define them real quick before we proceed.

alarm clock

Cron

Cron is a service that automatically starts at each boot of the Raspberry Pi and allows the user to execute scheduled commands.

Every minute, cron will watch if it’s tasked to run something, and if so, do it.
Today, we’ll discuss how to tell cron to execute our commands or scripts when needed.

Crontab

The crontab is a tool that allows us to list a task that we want to schedule, in a format that is understandable by the cron service.

A crontab will contain two things:
– the list of commands to run
– when to run them

Crontab is also a command. Here’s the syntax:
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]

What you need to know:

  • u: crontab opens the current user’s scheduler by default, but you can specify which one to open (if you have enough privileges).
  • l: list crontab content.
  • e: edit crontab content.
  • r: remove crontab content.

Most of the time, you’ll only need the commands “crontab -l” or “crontab -e”.

Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

Note: If you want to see task scheduling in action, I have a video lesson available just for community members. Join here to watch it directly! Becoming a member gives you access to 30+ other lessons for Raspberry Pi and comes with many other benefits.

Schedule a Task on Raspberry Pi

It’s time to take action. Now that you understand that crontab is a tool to let you make cron jobs, let’s learn how to schedule a task.

Your First Cron

Follow this procedure to schedule a task on your Raspberry Pi:

  • Edit the crontab with the command:
    crontab -e
  • On the first use, you need to choose an editor.
    I advise you to stay on Nano, so keep the default choice and hit enter:
    crontab choose default editor
  • That’s it. You’re now in the editor of crontab, which is empty and can be a little scary if it’s the first time you access it.

    Do not panic. I’ll explain what to do next.
  • All the lines starting with a # are comments and do nothing, so we can ignore them.
    Our changes will be added to the end of the file.
  • Move your cursor to the end of the file, and paste this line:
    * * * * * echo `date` >> /home/pat/log
    (replace ‘pat’ with your username)
  • Save the file (CTRL+O and Enter) and exit (Ctrl+X).

This simple line in the crontab will execute a command every minute, which is to write the date in a file. After a few minutes, the file will contain the dates of execution of the command.

You are probably wondering what the five stars you entered into crontab mean.

The syntax of an entry in the crontab is as follows:
1 2 3 4 5 command

  • 1: Minute (between 0 and 59)
  • 2: Hour (between 0 and 23)
  • 3: Day (between 1 and 31)
  • 4: Month (between 1 and 12)
  • 5: Day of week (between 0 and 7, starting on Sunday)

You can use a wildcard (*) for any or all of the time fields. So, * * * * * like we had above means run this command every minute of every hour of every day of every month and every day of the week.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.

Another Basic Cron Example

Now that you understand the theory, let’s look at a simple example to be sure it’s clear.

(credit: Red Hat Blog)

Imagine that you want to run a backup script every Wednesday at midnight. You must add a line like this:
0 0 * * 3 /home/pat/backup.sh

Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

The first two 0s means the time of 00:00 (midnight), and 3 for the day of the week (Wednesday).
Putting * for Day and Month means this command can apply every day of the month for every month of the year (but the final time field tells it to only be active on Wednesdays.)

In other words, use * for time fields you don’t care to specify.

More Cron Examples

There are then many possibilities to match the crontab with what you need.

  • Launch a script at fixed hours:
    0 6,12 * * * /home/pat/backup.sh
    => will run backup procedures at 6 and 12 only
  • Start a script every 2 hours:
    0 */2 * * * /home/pat/backup.sh
    => will run at 0, 2, 4, 6, …
  • Schedule a script only for the weekdays:
    0 3 * * 1-5 /home/pat/backup.sh
    => will not run on Saturday/Sunday
  • You can also start something on boot:
    @reboot /home/pat/backup.sh
    => will run at every boot

To give you an example, here are crons I’ve scheduled on my Pi Zero to control lights at home:

Adding Debug

Later in this article, you’ll learn how to debug a cron that doesn’t start at the time you’ve planned. But it may be easier to save the displayed messages or script errors in a file.

Let’s say you have a backup scheduled on your Raspberry Pi. Here are a few ways to create a log file with debug messages.

To do this, you must add one of these options in the crontab:

  1. To log what the script would display on the screen if you had launched it manually, you must specify the name of the file to write to with the ‘>’ character:
    @reboot /home/pat/backup.sh > /home/pat/log_backup.txt
    But the problem is that this option will overwrite the file on each run.
  2. So, if you want to add new lines to the end of the file, use ‘>>’ instead, like this:
    @reboot /home/pat/backup.sh >> /home/pat/log_backup.txt
  3. Now if you also want to log errors, you can write them to another file by adding this:
    @reboot /home/pat/backup.sh > /home/pat/log_backup.txt 2>/home/pat/errors_backup.txt
  4. And finally, if you’d rather save and display errors in the same file, do this:
    @reboot /home/pat/backup.sh > /home/pat/log_backup.txt 2>&1

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If this project doesn’t work as expected on your setup, don’t worry. You can get help directly from me and other Pi users inside the RaspberryTips Community. Try it for $1 and fix it together.

Crontab Tips & Tricks

You should be beginning to understand scheduling tasks on Linux by now, but unfortunately in IT, things rarely go as planned. Let me give you some tips to fix errors with cron jobs on Raspberry Pi.

User Privileges

A common mistake in scheduling tasks is to forget to consider the privileges of the user who will start the cron.

For example, this cron in the default user of the Raspberry will not work:
@reboot /usr/sbin/service ssh start

Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

You will get an error like this:
Failed to start ssh.service: Interactive authentication required.

If you use the current user’s crontab, the cron will execute with your current privileges.
Non-administrators are not allowed to start a service, so it won’t run.

For this to work, you must add this line in the root crontab (sudo crontab -e) or the global crontab found in /etc/crontab.

Related article: How to Easily Log In as Root on Raspberry Pi OS

Absolute Path

Another widespread mistake in using crons is ignoring the file path. You must specify the full path for it to work properly.

This scheduled task will not succeed, even when placed in the root crontab:
@reboot service ssh start

If you don’t specify the absolute path, cron cannot find the service file. So you must write /usr/sbin/service instead for this cron to run.

Also, pay attention to the content of your scripts. For example, if you have a PHP script that includes another file (ex: “include car.php”), and you add this script to the crontab, it won’t work.

You’ll need to add the absolute path in the function include or do something like this:
@reboot cd /var/www/html; /usr/bin/php test.php

This way, the include will be done in /var/www/html and the PHP script will find the car.php file.

Debugging

Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

In addition to what I wrote about earlier, there are two other methods that I will introduce to help you debug your crons.

Emails:

Cron will email the user if there’s a problem with his scheduled tasks in the crontab. If you have a mail server installed on your Raspberry Pi (as explained here), you can check the errors in your user’s email.

You can easily install one by doing:
sudo apt-get install mailutils

After that, you can type “mail” to read your emails.

If you have a well-configured email server, you can redirect emails to your email address by adding something like this to your crontab:
[email protected]

System Logs:

The system logs are another valuable tool to check what happened with your crons.
They used to be stored in /var/log/syslog, but now you must view them by using the journalctl command.

You can read system messages about cron by searching through the latest logs:
journalctl -f -n 50 | grep CRON

journalctl search crontab errors

It will show you the last errors, with real-time refresh if a new cron starts.

Now you know what a cron and a crontab are, how to schedule a task or a script on Raspberry Pi with many options and how to find out what didn’t work as you want.

Crons are something fundamental in Raspberry Pi and also used for auto-starting programs on Linux in general. I hope that you understand better how they work, it will serve you very often.

If you have doubts about planning a cron, know that there are websites that allow you either to create your planning or to check if what you did is what you wanted. For example, crontab.guru will do this for you.

Whenever you’re ready, here are other ways I can help you:

Test Your Raspberry Pi Level (Free): Not sure why everything takes so long on your Raspberry Pi? Take this free 3-minute assessment and see what’s causing the problems.

The RaspberryTips Community: Need help or want to discuss your Raspberry Pi projects with others who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct help (try it for just $1).

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts

2 Comments

  1. Can a crontab entry be set to run an application stored in /bin a certain number of minutes/seconds after boot?

    Actually want it to run AFTER network has been established AND the Pi has updated its real time

    1. Hi Neil,

      I don’t know if there is a better way to do this, but you can run two commands in a cron, so you can add a timer before the main command.
      Example: @reboot sleep 60;/bin/yourapp

      I hope it won’t wait to complete the boot, if it’s the case try another solution given on my article on how to auto start a program on boot.

Comments are closed.