Scheduling tasks using Cron: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
Line 53: Line 53:


[[File:Screen Shot 2021-12-27 at 5.14.36 am.png]]
[[File:Screen Shot 2021-12-27 at 5.14.36 am.png]]
* 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)
[[File:Screen Shot 2021-12-27 at 5.16.33 am.png]]

Revision as of 18:17, 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.

  • 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)