RasPi Series {6} – Cronjobs (Crontab) with the Raspberry Pi

To send our IP address automatically to the DNS server every 10 minutes, we will create a crontab.

In the last post, we wrote a Python script that configures your Dynamic DNS to automatically take over the IP address from the Raspberry pi.

We then uploaded this script to the Raspberry Pi. Now we only need to call it automatically.

What are cronjobs and what are they good for?

Cronjobs are tasks that you assign to a system that is always executed at a certain interval. For example, daily backups can be made through cron jobs, or you can retrieve your emails every 5 minutes. This feature is called crontab in the system of Raspberry Pi (Linux).

In this post we will learn how to create such a crontab and check if this so called cronjob is executed.

The crontab file

To edit the crontab file, just type the following into the command line:

crontab -e

What you see there might scare you off, but in reality is a very clear structure of the tasks that are regularly performed by your Raspberry Pi. I’ll bring the description to here, then we can handle that.

#┌ Minutes
#| ┌ Hours
#| | ┌ Day in month
#| | | ┌ Month
#| | | | ┌ Day in week
#| | | | |
 1 * * * * /path/to/script.sh

I’ve first simplified the standard example. What do you see? Only the column of minute is set to 1. This means that this script will always be executed when the minute on the clock is set to 1. If you type an asterisk (*) instead of a number, that means no matter what number. But in this case, the script is executed hourly, because the minute hand goes only hourly on the 1.

#┌ Minutes
#| ┌ Hours
#| | ┌ Day in month
#| | | ┌ Month
#| | | | ┌ Day in week
#| | | | |
 * * * * * /path/to/script.sh

The example above, shows you, what it looks like to get a script run every minute. This is, because it checks every minute to see if there are any scripts that need to be executed. The computer looks every minute at his clock / calendar and says: minutes? No matter. Hours? No matter. Month? No matter. Day of the week? No matter. Run!”

Day of the week

During the day of the week you can enter a value between 0 and 7 . Zero (0) and seven (7) both represent Sunday. The remaining days are normal Monday (0) to Saturday (6) .

Enter two or more values for a column

Let’s say there is a doctor who wants a backup of his system. Once in the morning (06:00 clock) and once after work (20:00).

Then you can specify these times simply separated by a comma. The implementation is really easy:

#┌ Minutes
#| ┌ Hours
#| |    ┌ Day in month
#| |    | ┌ Month
#| |    | | ┌ Day in week
#| |    | | |
 0 6,20 * * * /path/to/backupscript.sh

To do that every hour, we had to set the minutes to 0. If we leave an asterisk (*), our script would run between 6:00 am and 8:00 pm every minute.

Perform every x’th time

Let’s say you have a script that automates a quarterly settlement for you. You call this script in the desired time by simply writing * / 3 (stands for every 3 months).

#┌ Minutes
#| ┌ Hours
#| | ┌ Day in month
#| | | ┌ Month
#| | | |   ┌ Day in week
#| | | |   |
 0 0 0 */3 * /path/to/quarter.sh

You can apply this interval in each column.

Separate times

You also have fixed placeholders that do not need time to enter. You then write instead of the 5 numbers, just a text such as @reboot. It will look like this:

@reboot /path/to/script.sh

You can also enter the following placeholders:

  • @reboot
    At system startup
  • @yearly
  • @monthly
  • @weekly
  • @daily
  • @hourly

You have now learned the basics of creating a cronjob. Let’s do this now with our DynDNS script.

Using our script

First we need to connect to our Raspberry Pi and call the crontab file with crontab -e. If you have never done this command, a crontab file will be created for your user. There you can remove all commented lines ( Ctrl + Shift + K ).

Now we specify that our script must be executed every 10 minutes and at each boot:

*/10 * * * * python3 /home/pi/Cron/dyndns.py
@reboot python3 /home/pi/Cron/dyndns.py

We finish the editor ( Ctrl + X ) and confirm our changes. To check our cronjob we can either wait until the next run or enter the following in the console:

crontab -l

I hope that all of you have learned something in this post. I tried to explain it as understandably as possible. In the next post I will get to the readers, whose hoster does not support DynDNS. We will deal with free providers and how you can use them without Scripts ( FritzBox ).

Topics

automation DIY Linux Program python Raspberry Pi technology Tips

Beitrag teilen

WhatsApp it