Scheduling tasks using Cron

From Sensors in Schools
Jump to navigation Jump to search

Overview

  • In this lesson we are going to learn how to automate routine tasks on the Raspberry Pi.
  • This is helpful is ever you need to back up files, upload content to a web server or sample data from a sensor.
  • Cron is a program that was designed to schedule routine tasks (commands, scripts or programs) on Linux systems, such as the Raspberry Pi.
  • The command crontab is used to edit the list of scheduled tasks.
  • Each user on the computer has their own cron table, so you can think of cron as your own personal assistant.
  • In this lesson we will automate the running of the atm_sensor_get.py python program using cron.

Learning Objectives

  • Running python programs from the Terminal
  • Learn how to find the directory path to applications and programs on your computer
  • Learn how to use the text editing software nano
  • Learn how to create scheduled tasks in cron

Running python programs from the Terminal

  • In this example we are going to run the atm_sensor_get.py program using the Terminal.
  • Up to now, we have only run python programs from within the Python3 IDLE3 environment.
  • To run our atm_sensor_get.py program we will use the command python3.

Open the Terminal and navigate to your project’s directory

  • From the Raspberry Pi top bar menu click on the Terminal icon.
  • Once in your default home directory (/home/pi) enter ls to list all contents.
  • Find your project’s directory (e.g. botanica-park-lake) and use the change directory (cd) command to enter the directory (e.g. cd botanica-park-lake)
  • Enter the command ls to list all directory contents.

  • You should see the python file atm_sensor_get.py
  • To run this file, enter the command python3 atm_sensor_get.py
    • python3 – name of application
    • atm_sensor_get.py – script or program to run
  • The printouts we can see in the Terminal are the outputs from print() statements within our python code.

Finding the directory path to applications and python programs

  • In this example we will find the path of the python3 application and scratch on the Raspberry Pi.
  • A path shows the location where a software application can be found within the directory structure on your computer.
  • To find the path for an application we use the Terminal command which followed by the name of the software application.

Open the Terminal and navigate to your project’s directory

  • From the Raspberry Pi top bar menu click on the Terminal icon.
  • Once in your default home directory (/home/pi) enter ls to list all contents.
  • Find your project’s directory (e.g. botanica-park-lake) and use the change directory (cd) command to enter the directory (e.g. cd botanica-park-lake)
  • Enter the command ls to list all directory contents.

  • To find path to a current directory enter the command pwd (present working directory).
  • This is helpful when we need to know the path to a program we have written.
  • In this case, the path to the botanical-park-lake directory is /home/pi/botanica-park-lake
  • The full path to the atm_sensor_get.py program is /home/pi/botanica-park-lake/atm_sensor_get.py

  • We can find the path to other commonly used Linux commands.
  • For example, the command ls is also an application. To find its path enter which ls
  • The directory path to the ls application is found in the /bin directory (short for binary files)

  • Try this with other commands such as cat, cd, touch and pwd.
  • Do you see a pattern?
  • You can find other commands here: Linux commands - ["Linux Commands"]
  • You can also find the path to other programming applications, such as Scratch
  • Enter the command which scratch

Finding an application or file within a directory

  • Sometimes when we use the Terminal command which to get the path for an application, we also want to navigate to the directory to see the application within the directory.
  • To navigate to the /usr/bin directory enter the command cd /usr/bin

  • To list all the files and directories enter the command ls
  • Unfortunately, in this directory there are more than 1000 files (1335 files to be exact).
  • You can count the number of files with the command ls -1 | wc
    • ls -1 - this command lists all files and directories in one column (number one -1, not the letter L)
    • | - this is the pipe symbol (above the Enter key – is a vertical line). It takes the output from one command and makes it the input for another command.
    • wc - this command is short for Word Count

Narrowing down file search results using the Terminal command grep

  • We can narrow our search down a little using another command called grep
  • In the same directory enter the command ls -1 | grep python3
  • This command will highlight only those entries that have the file name python3
    • ls -1 - list all files as a single column (-1 option)
    • | - pipe symbol
    • grep python3 - search for file names containing the text “python3”

  • Using the | pipe symbol we can also do a word count at the very end to see how many python3 files there were with the command ls -1 | grep python3 | wc
  • As you can see, the | pipe symbol is very handy.

Using the terminal text editor nano

  • Nano is a text file editor that is used for creating and modifying text-based files.
  • It is a simple editor and is widely used on Linux platforms.
  • We need to learn how to use nano so that we can write scheduling tasks.
  • In this example we will use nano to create a new file, add some text to the file and then save the file.

Create a new file using nano

  • Navigate to your project directory using the Terminal command 'cd
  • To create an empty file named test1.txt that can be edited in nano enter the command nano test1.txt

  • This will open the nano test editor to an empty document.

  • Enter text using the keyboard. You won’t be able to use the mouse.
  • To navigate simply use the keyboard arrow keys.


Saving files in nano

  • When you have finished editing your document enter CTRL+X keys to Exit.
  • You will be prompted to Save your file.
  • Enter y for yes and press Enter. Lowercase y is fine.

  • You will be asked if the file is to be saved as test1.txt
  • Accept the name test1.txt and press Enter.

  • When the file is saved you will be returned to the main Terminal window.

Learn how to create scheduled tasks in cron

  • Cron is a tool to configure scheduled tasks in Linux.
  • In this example we will use the command crontab -e to create one scheduled task.
  • We will configure cron to run our python program atm_sensor_get.py once every hour so that we can start to collect atmospheric data.

Running cron for the first time

  • To run Cron enter the Terminal command crontab -e
  • The first time you run crontab you will need to select an editor.
  • Choose the relevant number to select nano and press Enter.
  • Your selection options may different to those shown below.

  • When the Cron table opens you should see the following comments (#) in the first few lines of the file.
  • Comments will not be executed. If required, they can be deleted to make the cron table file less cluttered.

Setting when to run cron task

  • The makeup for a cron entry is made up of 6 components:
    • minute (0-59)
    • hour (0-23)
    • day of month (1-31)
    • month of year (1-12)
    • day of week (0-6 – Sunday to Saturday)
    • command to be executed
  • If a number is replaced with an * (asterix), then that rule applies for every unit
  • Here are some examples related to the set time for a task to run:
    • 10 2 * * * – 2:10am every day
    • 00 15 2 * * - 3pm on the 2nd of every month
    • 05 10 * * * - 10:05am every day
    • 03 05 * * * - 5:03am every day
    • 00 15 * * 5 – Every Thursday at 3:00pm
    • 05 * * * * - 5 minutes past the hour, every hour
  • We will run our program at 5 minutes past the hour, every hour so the time rule is 05 * * * *

Entering the command for a cron task

  • We would like to run our atm_sensor_get.py file using Python3.
  • The full command would therefore be python3 /home/pi/botanica-park-lake/atm_sensor_get.py
  • The full path needs to be given for our python program atm_sensor_get.py
  • We could also give the full path to the python3 application.
  • In this case the full command line would be /usr/bin/python3 /home/pi/botanica-park-lake/atm_sensor_get.py

Creating a complete cron entry

  • Now that we have all the details we need for our cron entry we can enter it in one line
  • Scroll using the arrow keys to the last entry in the Cron table
  • Enter the following line: 5 * * * * python3 /home/pi/botanica-park-lake/atm_sensor_get.py
  • This will then run the python program at 5 minutes past the hour, every hour.

Saving and viewing cron table entries

  • To save your cron table, use the same method as for the nano application.
  • Save with CTRL+X.
  • Then reply y for Yes.
  • And then press Enter to accept the default file name.

  • To view all your currently saved scheduled tasks in cron enter the Terminal command crontab -l

Checking to see if cron is working

  • When the atm_sensor_get.py program runs it automatically updates the data.txt file.
  • You can see the last time this file was updated by either entering the ls -l command.
  • This would show the date and time the file was last updated.

  • Alternatively, you can enter cat data.txt and look at the last time the file was modified.
  • If there are many entries in the data.txt file, you can print out the most recent entries at the end of the file with the command tail data.txt

  • Congratulations on completing this lesson.
  • In our next lesson we will start to graph our data using plotly.