Scheduling tasks using Cron: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
Line 45: Line 45:
* Enter the command '''ls''' to list all directory contents.
* Enter the command '''ls''' to list all directory contents.


[[]]
[[File:Screen Shot 2021-12-27 at 5.10.34 am.png]]

Revision as of 18:11, 26 December 2021

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.