Scheduling tasks using Cron

From Sensors in Schools
Revision as of 06:51, 26 December 2021 by EdmondLascaris (talk | contribs) (Created page with "== 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 t...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.