From the course: Raspberry Pi Weekly
Cron on the RPI - Raspberry Pi Tutorial
From the course: Raspberry Pi Weekly
Cron on the RPI
- It would be really handy to have a way to run a program on a periodic basis, for example, once per day or once per week or very five minutes. Linux, and Raspbian, provides cron for just that reason. Let's take a look at how you use cron to trigger an event at a certain time or repeatedly. Hi, I'm Mark Niemann-Ross and welcome to this week's edition of Raspberry Pi Weekly. Every week, we explore the Raspberry Pi and share useful tips. Cron requires two things. An application to run and instructions for when to run it. For our demonstration, I've created a Python program that blinks an LED five times. Instructions for this very simple project are in the handout. You'll find the program in the exercise files as cron_blink.py. This is cron_blink.py and it doesn't matter where the program is located but you'll need a complete path name for it. Be careful about path names that include spaces, such as the space in Exercise Files. I've moved cron_blink.py to the Documents folder to avoid any spaces in the path name. To obtain the path name, I can right click on the icon and then go to Copy Paths. This will give me a complete path name to the application. To check the path name, I can open up a terminal window and use Edit, Paste. You can see that my cron_blink is located in the home directory /pi/Documents. Now, I can test that. I have to put in an application that's going to run the code. In this case, it's a Python 3 program so I type in python3 space and when I hit Return, that program should run. If it doesn't run here, it's not going to run in cron. Once you have a path name to an executable file, you'll need a time to run it. Cron is rich with ways to specify the time to run, which makes it confusing. I've found a very helpful website at crontab.guru. I'll show you where to put these commands in in just a minute but first, let's examine the command. To run a command every Monday at one 'clock in the afternoon, you'd type in 013, for one o'clock, star, star and a one and you can see at the very top line, crontab.guru has identified this as every one o'clock on Mondays. To run a command every five minutes, use slash. So for example, star /5 star, star, star, star runs a command every fifth minutes. Crontab.guru also supplies some random times as well as some examples. You'll notice that the smallest cron increment is for minutes, not seconds. Cron only checks for commands every minute so anything less, for example, every three seconds, would be pointless. When you have both a command and a cron time specifier, you're ready to enter that into crontab which is the table of cron entries. You can do that with crontab -e. Crontab will ask you which editor you want to use. In this case, I'm going to use nano, which is number one. I'm going to scroll down to the bottom of crontab and enter a command to run our cron_blink.py every five minutes. So in this case, I'll type in star /5 star space star space star space star, followed by python3, which is the command to run and then the application path. In this case, I'm going to paste in the path name to cron_blink.py. Running a program with cron happens entirely in the background and any text you print to screen won't appear. So you'll want a way to confirm your program is running. Do this by sending messages to a log file you can periodically check. In my case, I'm going to use the greater than sign and send it to /home/pi/mylogfile.txt. If you also want to capture error messages from the system, add two greater than sign, ampersand one after the command. This directs error messages into the first file. In this command, use of one greater than sign will overwrite the file each time. If you use two greater than signs, Linux will append commands to the file. This is great if you want to keep a running log but may create a huge file if you forget to occasionally delete the file. When you're finished, use Control + O to write the file out and Control + X to exit nano. Then reboot your Raspberry Pi. When your Raspberry Pi is back running, there are a couple of ways to confirm that is cron is actually working. Type in crontab -l. And that will list the current crontab. You can see that our command is at the bottom. Crontab only tells you if a job is scheduled, not if it ran successfully. For that, check your text file log for diagnostic messages. You might also find useful debugging tips in the syslog. Try using grep to filter through this rather large file. Sometimes cron jobs can be frustrating. And there is a well-written description of troubleshooting cron jobs at garyhall.org.uk. On a side note, several posts on the internet suggest using @reboot as a way of starting a command when the Raspberry Pi reboots and this is a bad idea. Number one, it won't work on shutdown or boot, only reboot. It may not work on some versions of Linux and you may have issues with root users versus just standard users. There are additional videos on using cron with Linux in this library. Check them out for in-depth discussions of cron and crontab. Thanks for joining me for this episode of Raspberry Pi Weekly. Be sure to join the LinkedIn group and check out previous episodes on LinkedIn Learning. I'll see you next week with more Raspberry Pi adventures.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
The Raspberry Pi family3m 53s
-
(Locked)
GPIO male to female jumpers3m 30s
-
(Locked)
Weatherproofing Pi4m 19s
-
(Locked)
Power and batteries3m 57s
-
(Locked)
IR rangefinder project2m 26s
-
(Locked)
Clock project6m 20s
-
(Locked)
Wire a stepper motor3m 38s
-
Code a stepper motor3m 37s
-
(Locked)
gpiozero library4m 50s
-
(Locked)
Take a screenshot4m 26s
-
(Locked)
Analog to digital2m 38s
-
(Locked)
Autostart file setup4m 24s
-
(Locked)
Simple connection to a speaker2m 43s
-
(Locked)
Red LED, RGB LED, blink, and glow3m 23s
-
Connect input to output: Range finder to LED2m 4s
-
(Locked)
Raspberry Pi Desktop on a laptop4m 10s
-
(Locked)
Programming with Node-RED6m 15s
-
(Locked)
Programming IoT with IFTTT5m 24s
-
(Locked)
Update the Raspberry Pi with apt-get3m 3s
-
(Locked)
A clean and simple web server5m 23s
-
(Locked)
Use SSH to control the Raspberry Pi3m 28s
-
(Locked)
Camera control with raspistill4m
-
(Locked)
GPIO pinout charts3m 53s
-
(Locked)
Back up the Raspberry Pi3m 1s
-
(Locked)
Use SmartSim to explore logic circuits4m 47s
-
(Locked)
Raspberry Pi 46m 29s
-
(Locked)
Change your default password2m 44s
-
(Locked)
Mathematica and general-purpose input/output (GPIO)6m 7s
-
(Locked)
Build a Raspberry Pi private cloud8m 40s
-
Dealing with undervoltage3m 50s
-
(Locked)
Play music with Sonic Pi5m 53s
-
(Locked)
Add a printer to Raspberry Pi3m 38s
-
(Locked)
Use Screenly for web kiosks5m 59s
-
(Locked)
Use raspivid for Raspberry Pi Videos4m 55s
-
(Locked)
Build an MQTT publisher6m 33s
-
(Locked)
Build an MQTT client4m 5s
-
(Locked)
Connect to MQTT with Node-RED9m 55s
-
(Locked)
Build an MQTT broker4m 44s
-
(Locked)
A power switch for the Raspberry Pi3m 57s
-
(Locked)
Input polling vs. interrupts5m 41s
-
(Locked)
GPIO Pi headers2m 34s
-
(Locked)
High-grade audio for the Raspberry Pi4m 34s
-
(Locked)
Program the Sense HAT with Node-RED6m 54s
-
(Locked)
Remote GPIO6m 5s
-
(Locked)
Framboisedorf4m 6s
-
(Locked)
Use Kano OS5m 15s
-
(Locked)
What's new with Scratch 3.0?2m 57s
-
(Locked)
Controlling high voltages with the Raspberry Pi4m 53s
-
(Locked)
Rotary phone: Intro and switches3m 12s
-
(Locked)
Rotary phone: Ringer2m 44s
-
(Locked)
Rotary phone: Speaker5m 28s
-
(Locked)
Piscope5m 33s
-
Cron on the RPI7m 45s
-
(Locked)
Keep your plants watered with a Raspberry Pi4m 42s
-
(Locked)
Grove HAT4m 45s
-
(Locked)
Build a door cam with a Raspberry Pi4m 4s
-
(Locked)
Explore the Sense HAT emulator3m 49s
-
(Locked)
Use a hard drive with the Raspberry Pi3m 33s
-
(Locked)
Build a temperature-controlled fan for the Raspberry Pi 46m 59s
-
(Locked)
Run COBOL on a Raspberry Pi4m 12s
-
(Locked)
Adafruit IO7m 41s
-
(Locked)
Overclocking the Raspberry Pi7m 4s
-
(Locked)
The boot directory and config.txt7m
-
(Locked)
Field connection to the Raspberry Pi6m 30s
-
(Locked)
Rotary phone: Microphone hardware4m 34s
-
(Locked)
Rotary phone: Microphone software5m 47s
-
(Locked)
Raspberry Pi 12-megapixel camera7m 27s
-
(Locked)
Text to speech on a chip5m 44s
-
(Locked)
Connect GPS to the Raspberry Pi7m 47s
-
(Locked)
Minecraft and GPIO5m 11s
-
(Locked)
Raspberry Pi Pico5m 31s
-
(Locked)
Use libgpiod instead of sysfs9m 51s
-
(Locked)
Revisiting adding a printer to Raspbian3m 50s
-
(Locked)
Revisiting a temperature controlled fan4m 26s
-
(Locked)
Run R on a Raspberry Pi7m 35s
-
(Locked)
Another motion-activated camera4m 49s
-
(Locked)
Ubuntu desktop4m 38s
-
(Locked)
A web interface to a camera5m 53s
-
(Locked)
Use systemd to start programs at boot6m 55s
-
(Locked)
Find the IP address of your Raspberry Pi5m 2s
-
(Locked)
Contribute to community computing4m 6s
-
(Locked)
BBC BASIC on the Raspberry Pi4m 25s
-
(Locked)
Capacitive touch sensor3m 35s
-