Scheduling tasks using Cron: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 97: | Line 97: | ||
[[File:Screen Shot 2021-12-27 at 5.34.49 am.png]] | [[File:Screen Shot 2021-12-27 at 5.34.49 am.png]] | ||
== 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''' | |||
[[File:Screen Shot 2021-12-27 at 5.36.57 am.png]] | |||
* This will open the '''nano test editor''' to an empty document. | |||
[[File:Screen Shot 2021-12-27 at 5.38.06 am.png]] | |||
* Enter text using the keyboard. You won’t be able to use the mouse. | |||
* To navigate simply use the '''keyboard arrow keys'''. | |||
[[File:Screen Shot 2021-12-27 at 5.39.15 am.png]] | |||
Revision as of 18:39, 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.
- 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.
- 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.








