Close Menu
    What's Hot

    How to Auto Start a Program on Raspberry Pi? (4 Ways)

    8 September، 2024

    List of Best Hacking Software For Windows, Linux, and Mac OS X

    7 September، 2024

    The rpm command

    20 December، 2022

    Copy virtual machine to the new host

    20 December، 2022
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    keepuptechnologykeepuptechnology
    • Home
      • Linux
      • Network
      • raspberry pi
      • mqtt
      • esp8266
    • Linux

      List of Best Hacking Software For Windows, Linux, and Mac OS X

      7 September، 2024

      The rpm command

      20 December، 2022

      Copy virtual machine to the new host

      20 December، 2022

      Remove password from Virtual Machine Manager on Linux

      20 December، 2022

      How to create FAT32 USB drive on Linux

      20 December، 2022
    • Network

      Wifi Pineapple Mark V on TP-Link MR3020

      10 September، 2022
    • IOT
      • esp8266
      • mqtt
      • raspberry pi
    • All
      1. IOT
      2. raspberry pi
      3. Linux
      4. View All

      How to Auto Start a Program on Raspberry Pi? (4 Ways)

      8 September، 2024

      How to Install VMWare ESXi on a Raspberry Pi? (Step by step)

      20 December، 2022

      Getting started with Proxmox on Raspberry Pi (Virtualization server)

      20 December، 2022

      How To Create USB Rubber Ducky With Raspberry Pi Pico

      24 November، 2022

      How to Auto Start a Program on Raspberry Pi? (4 Ways)

      8 September، 2024

      How to Install VMWare ESXi on a Raspberry Pi? (Step by step)

      20 December، 2022

      Getting started with Proxmox on Raspberry Pi (Virtualization server)

      20 December، 2022

      How To Create USB Rubber Ducky With Raspberry Pi Pico

      24 November، 2022

      List of Best Hacking Software For Windows, Linux, and Mac OS X

      7 September، 2024

      The rpm command

      20 December، 2022

      Copy virtual machine to the new host

      20 December، 2022

      Remove password from Virtual Machine Manager on Linux

      20 December، 2022
    • Contact Us
    Subscribe
    keepuptechnologykeepuptechnology
    Home » How to Schedule a Task on a Raspberry Pi?

    How to Schedule a Task on a Raspberry Pi?

    0
    By admin on 10 October، 2022 raspberry pi
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Scheduling commands or scripts on a Raspberry Pi, and on Linux generally, is not easy for a beginner.
    There are many tips you should know to make it work every time, and we will see them in detail.

    To schedule a task on Raspberry Pi, there is a tool name “crontab”.
    This tool is useful to run a script at a specific time or on boot.

    What is this thing? I will explain to you 🙂

    What is crontab?

    Cron

    Cron is a service, automatically started at each boot of the Raspberry Pi, which allows the user to execute scheduled commands.

    Every minute, cron will watch if he has to do something and do it.
    What we’re going to see today is how to tell cron to execute our command or script when needed.

    Crontab

    A crontab is a tool that will allow us to list what we want to start, in a format 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 is opening the current user crontab by default, but you can specify which one to open (if you have enough privileges)
    • l: list current crontab content
    • e: edit current crontab content
    • r: remove current crontab content

    Most of the time, you will only use the commands “crontab -l” or “crontab -e”.

    Schedule a task on Raspberry Pi

    Add a cron

    It’s time to take action.
    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 :pi@raspberrypi:~ $ crontab -e
      no crontab for pi - using an empty one
      Select an editor. To change later, run 'select-editor'.
      /bin/ed
      /bin/nano <---- easiest
      /usr/bin/vim.tiny
      Choose 1-3 [2]:
    • That’s it. You are 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
    • All the lines starting with a # are comments and do nothing.
      We can ignore them.
      Our changes will be done at the end of the file
    • Move your cursor to the end of the file, and paste this line:
      * * * * * echo `date` >> /home/pi/log
    • Save the file (CTRL+O and Enter) and exit (Ctrl+X)

    This simple line in the crontab will allow us to execute a command every minute, which will 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 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)

    Basic example

    Now that you understand the theory, let’s look at a simple example to be sure it’s clear.
    Imagine that you want to run a backup script every Wednesday at midnight.
    You must add a line like this :

    0 0 * * 3 /home/pi/backup.sh

    Midnight for the two first 0, and 3 for the day of the week (Wednesday).

    Other examples

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

    • Launch a script at fixed hours0 6,12 * * * /home/pi/backup.sh => will run at 6 and 12 only
    • Start a script every 2h0 */2 * * * /home/pi/backup.sh => will run every 2hours (so 0, 2, 4, 6, …)
    • Schedule a script only during the weekdays0 3 * * 1-5 /home/pi/backup.sh => will not run on Saturday/Sunday
    • You can also start something on boot@reboot /home/pi/backup.sh => will run at every boot

    Adding debug

    We will see at the end of the article how to debug a cron that does not start, or not at the time you have planned.
    But it may be easier to save the displayed messages or script errors in a file.

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

    1. To log in a file what the script would have displayed on the screen if you had launched it manually, you must specify the name of the file with the character “>” :
    @reboot /home/pi/backup.sh > /home/pi/log_backup.txt

    But this option will erase the file at each run

    1. So if you want to add a new line at the end of the file, you have to add the character “>>”, like this:
    @reboot /home/pi/backup.sh >> /home/pi/log_backup.txt
    1. Now if you want to log errors in another file you have to add this :
    @reboot /home/pi/backup.sh > /home/pi/log_backup.txt 2>/home/pi/errors_backup.txt
    1. And finally, if you want to save errors and the displayed in the same file, you can do this :
    @reboot /home/pi/backup.sh > /home/pi/log_backup.txt 2>&1

    ou should be starting to understand the little tricks by now, but unfortunately in IT things rarely happen as expected.
    I will give you some tips to fix the errors with the crons on your Raspberry Pi.

    Crontab tips

    User privileges

    A common mistake in creating crons 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 (“pi”) will not work :

    @reboot /usr/sbin/service ssh start

    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 run with your current privileges.
    Pi is not allowed to start a service, so it can’t work.

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

    Absolute path

    Another widespread mistake using crons is to ignore the file path.
    You must use the full path to make it work properly.

    This cron will not work, even in the root crontab :

    @reboot service ssh start

    If you do not specify the absolute path, cron will not know where the service file is.
    So you have to write /usr/sbin/service to make this cron work.

    Also pay attention to the content of your scripts.
    For example, if you have a PHP script that includes another file (ex: include “file.php”), and you add this script to the crontab, it will not work.
    You will 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 file “file.php”.

    Debugging

    In addition to what I wrote above, there are two other methods that I will introduce to debug your crons.

    Emails :

    Cron will email the user if there is a problem with one of 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 the email file of your user.

    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 :
    MAIL=yourname@provider.com

    Syslog :

    Syslog is another valuable help to check what happened with your crons.
    It’s a log file located in /var/log/syslog.

    You can read the last messages about crons with this command :

    tail -f /var/log/syslog | grep CRON

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

    Conclusion

    Now you know what a cron and a crontab is, 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 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.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleHow Does the Raspberry Pi File System Works?
    Next Article How to Install an Email Server on your Raspberry Pi?
    admin
    • Website

    Related Posts

    How to Auto Start a Program on Raspberry Pi? (4 Ways)

    8 September، 2024

    How to Install VMWare ESXi on a Raspberry Pi? (Step by step)

    20 December، 2022

    Getting started with Proxmox on Raspberry Pi (Virtualization server)

    20 December، 2022
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Keep Up Technologies. Articles and topics to keep up with computer science and technology
    We're social. Connect with us:

    Facebook X (Twitter) Instagram Pinterest YouTube
    Top Insights
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2025 Makrof. Designed by Makrof.

    Type above and press Enter to search. Press Esc to cancel.