Skip to main content
Engineering LibreTexts

The Linux crontab, scheduling events

  • Page ID
  • Scheduling data collection events is critical for highly automated IoT systems.

    Programs can be scheduled to run at boot time or anytime throughout the day.


    The @ schedules:
    # string             meaning
    # ------           -----------
    # @reboot       Run once, at startup.
    # @yearly       Run once a year, "0 0 1 1 *".
    # @annually     (same as @yearly)
    # @monthly      Run once a month, "0 0 1 * *".
    # @weekly       Run once a week, "0 0 * * 0".
    # @daily        Run once a day, "0 0 * * *".
    # @midnight     (same as @daily)
    # @hourly       Run once an hour, "0 * * * *".

    The above @ schedules demonstrate the shorthand for some event timing.

    For example @hourly is equivalent to "0 * * * *"


    Examples of cronjobs:

    1. I want to know if the power goes out where I live.  If the power fails then the RPi does not send a marker to the cloud server at the scheduled time.  My Android phone is programmed to check the existence of the marker on the server.  If the marker exists it deletes the marker.  If the marker does not exist I get a text to speech message on the phone indicating that a failure occurred.
    2. For some networks the RPi gets a different IP when it boots up.  To avoid having to connect a monitor and keyboard to get the new IP a boot cron is used to email the new IP to the user.


    # Check to see if a crontab already exists.
     crontab -l
    # If a crontab already exists then make a backup so that any previous work is not lost.
    crontab -l >

    If a crontab does not exist for the user the following message is displayed.


    The above output is from an old Red Hat server, output for a different OS may not be the same.


    There are two ways to approach editing of a crontab.

    1. Edit the crontab directly
    2. Edit a file containing the cron-table then initialize the crontab with the file.


    The following is an example of editing the crontab directly.

    Note; for the first time edit the choice of editors is required.



    In the above example saving the edits makes the changes active.

    However there is no backup of your cron-table.

    The following method makes a backup.

    crontab -l > crontab.bak


    Alternatively start with a cron-table file, edit that file, and make the data in new table file active.

    The following command puts the content of cron-table file (crontabRPi.scr) in the active crontab.

    crontab crontabRPi.scr
    # Confirm the changes.
    crontab -l

    A well-documented cron-table file named crontabRPi.scr can be downloaded here;


    Note, there are no active lines in this file (they are commented out).  Activate any lines of interest by removing the comment ‘#’, saving the file, and activating the changes (crontab crontabRPi.scr)

    This second method insures that there always exists a document of the current crontab (crontabRPi.scr). This document will be saved with any backup of the user account data.


    Issues to be aware of;

    In most cases the full path to the application is required.

    The full path to the input and output files is always required.  This is because the crontab has no knowledge of the user’s path.

    pi@serverNine$ cat
    # For scripts run on the command line the relative path to the data is best
    # The directory containing the script can be copied and the script run.
    touch ./touch.tmp
    # For scripts run through the crontab the full path must be
    #  given to all input and output files.
    mkdir -p /home/pi/temperatureSensor
    touch /home/pi/temperatureSensor/touch.tmp

    In the example script above the “touch” command is known to be in the crontab path.  If it was not in the path or we did not know it was in the path then give the full path to the program “touch”.

    pi@serverNine$ which touch


    mkdir -p /home/pi/temperatureSensor
    /usr/bin/touch /home/pi/temperatureSensor/touch.tmp


    Useful patterns:
    # minute (0-59),
    #    |
    #    |
    #   Mins 
    # Run the command at the top and bottom of the hour


    # hour (0-23),
    #   |
    #   |
    # Hours
    # Only when I am sleeping


    #     hour (0-23),
    #        |
    #        |
    #      Hours
    # Every other hour, odd hours


    #     hour (0-23),
    #        |
    #        |
    #      Hours
    # Every other hour, even hours


    #     hour (0-23),
    #        |
    #        |
    #      Hours
    #  Every two hours


    # minute (0-59),
    #  |
    #  |
    # Mins
    # Every 10 minutes


    # day of the week (0-7 with 0=7=Sunday).
    #   |
    #   |
    # Day of
    # the week
    # Monday through Saturday, not Sunday


    Cron examples can be found here;


    • Was this article helpful?