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.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
The Raspberry Pi family3 分钟 53 秒
-
(已锁定)
GPIO male to female jumpers3 分钟 30 秒
-
(已锁定)
Weatherproofing Pi4 分钟 19 秒
-
(已锁定)
Power and batteries3 分钟 57 秒
-
(已锁定)
IR rangefinder project2 分钟 26 秒
-
(已锁定)
Clock project6 分钟 20 秒
-
(已锁定)
Wire a stepper motor3 分钟 38 秒
-
Code a stepper motor3 分钟 37 秒
-
(已锁定)
gpiozero library4 分钟 50 秒
-
(已锁定)
Take a screenshot4 分钟 26 秒
-
(已锁定)
Analog to digital2 分钟 38 秒
-
(已锁定)
Autostart file setup4 分钟 24 秒
-
(已锁定)
Simple connection to a speaker2 分钟 43 秒
-
(已锁定)
Red LED, RGB LED, blink, and glow3 分钟 23 秒
-
Connect input to output: Range finder to LED2 分钟 4 秒
-
(已锁定)
Raspberry Pi Desktop on a laptop4 分钟 10 秒
-
(已锁定)
Programming with Node-RED6 分钟 15 秒
-
(已锁定)
Programming IoT with IFTTT5 分钟 24 秒
-
(已锁定)
Update the Raspberry Pi with apt-get3 分钟 3 秒
-
(已锁定)
A clean and simple web server5 分钟 23 秒
-
(已锁定)
Use SSH to control the Raspberry Pi3 分钟 28 秒
-
(已锁定)
Camera control with raspistill4 分钟
-
(已锁定)
GPIO pinout charts3 分钟 53 秒
-
(已锁定)
Back up the Raspberry Pi3 分钟 1 秒
-
(已锁定)
Use SmartSim to explore logic circuits4 分钟 47 秒
-
(已锁定)
Raspberry Pi 46 分钟 29 秒
-
(已锁定)
Change your default password2 分钟 44 秒
-
(已锁定)
Mathematica and general-purpose input/output (GPIO)6 分钟 7 秒
-
(已锁定)
Build a Raspberry Pi private cloud8 分钟 40 秒
-
Dealing with undervoltage3 分钟 50 秒
-
(已锁定)
Play music with Sonic Pi5 分钟 53 秒
-
(已锁定)
Add a printer to Raspberry Pi3 分钟 38 秒
-
(已锁定)
Use Screenly for web kiosks5 分钟 59 秒
-
(已锁定)
Use raspivid for Raspberry Pi Videos4 分钟 55 秒
-
(已锁定)
Build an MQTT publisher6 分钟 33 秒
-
(已锁定)
Build an MQTT client4 分钟 5 秒
-
(已锁定)
Connect to MQTT with Node-RED9 分钟 55 秒
-
(已锁定)
Build an MQTT broker4 分钟 44 秒
-
(已锁定)
A power switch for the Raspberry Pi3 分钟 57 秒
-
(已锁定)
Input polling vs. interrupts5 分钟 41 秒
-
(已锁定)
GPIO Pi headers2 分钟 34 秒
-
(已锁定)
High-grade audio for the Raspberry Pi4 分钟 34 秒
-
(已锁定)
Program the Sense HAT with Node-RED6 分钟 54 秒
-
(已锁定)
Remote GPIO6 分钟 5 秒
-
(已锁定)
Framboisedorf4 分钟 6 秒
-
(已锁定)
Use Kano OS5 分钟 15 秒
-
(已锁定)
What's new with Scratch 3.0?2 分钟 57 秒
-
(已锁定)
Controlling high voltages with the Raspberry Pi4 分钟 53 秒
-
(已锁定)
Rotary phone: Intro and switches3 分钟 12 秒
-
(已锁定)
Rotary phone: Ringer2 分钟 44 秒
-
(已锁定)
Rotary phone: Speaker5 分钟 28 秒
-
(已锁定)
Piscope5 分钟 33 秒
-
Cron on the RPI7 分钟 45 秒
-
(已锁定)
Keep your plants watered with a Raspberry Pi4 分钟 42 秒
-
(已锁定)
Grove HAT4 分钟 45 秒
-
(已锁定)
Build a door cam with a Raspberry Pi4 分钟 4 秒
-
(已锁定)
Explore the Sense HAT emulator3 分钟 49 秒
-
(已锁定)
Use a hard drive with the Raspberry Pi3 分钟 33 秒
-
(已锁定)
Build a temperature-controlled fan for the Raspberry Pi 46 分钟 59 秒
-
(已锁定)
Run COBOL on a Raspberry Pi4 分钟 12 秒
-
(已锁定)
Adafruit IO7 分钟 41 秒
-
(已锁定)
Overclocking the Raspberry Pi7 分钟 4 秒
-
(已锁定)
The boot directory and config.txt7 分钟
-
(已锁定)
Field connection to the Raspberry Pi6 分钟 30 秒
-
(已锁定)
Rotary phone: Microphone hardware4 分钟 34 秒
-
(已锁定)
Rotary phone: Microphone software5 分钟 47 秒
-
(已锁定)
Raspberry Pi 12-megapixel camera7 分钟 27 秒
-
(已锁定)
Text to speech on a chip5 分钟 44 秒
-
(已锁定)
Connect GPS to the Raspberry Pi7 分钟 47 秒
-
(已锁定)
Minecraft and GPIO5 分钟 11 秒
-
(已锁定)
Raspberry Pi Pico5 分钟 31 秒
-
(已锁定)
Use libgpiod instead of sysfs9 分钟 51 秒
-
(已锁定)
Revisiting adding a printer to Raspbian3 分钟 50 秒
-
(已锁定)
Revisiting a temperature controlled fan4 分钟 26 秒
-
(已锁定)
Run R on a Raspberry Pi7 分钟 35 秒
-
(已锁定)
Another motion-activated camera4 分钟 49 秒
-
(已锁定)
Ubuntu desktop4 分钟 38 秒
-
(已锁定)
A web interface to a camera5 分钟 53 秒
-
(已锁定)
Use systemd to start programs at boot6 分钟 55 秒
-
(已锁定)
Find the IP address of your Raspberry Pi5 分钟 2 秒
-
(已锁定)
Contribute to community computing4 分钟 6 秒
-
(已锁定)
BBC BASIC on the Raspberry Pi4 分钟 25 秒
-
(已锁定)
Capacitive touch sensor3 分钟 35 秒
-