Run a sudo (or startup) python script from cron on a Raspberry Pi?
Drop the sudo
its not needed since its running as root, and sudo by default wont run without a tty.
You can tell sudo to run without a tty by running visudo
and commenting out requiretty:
#Default requiretty
NOTE this does have security disadvantages see here
nivek
Updated on September 18, 2022Comments
-
nivek over 1 year
I'm helping out with an art project (http://stargateeggbeater.com/ for those interested) built on Raspberry Pi. The RPi controls an addressable LED strip through the GPIO, using the
spidev
device per these instructions. To start the display, we run:sudo python lightpaint-FF.py
We plan to take this project to parties and festivals, so ideally we'd like to have it start running automatically on boot. (Our RPi doesn't have a monitor connected, so right now we have to log in with another laptop using SSH and run the code using
nohup
before logging out again).My first attempt at solving this problem was to write a simple bash script:
#!/bin/bash if [ ! "$(pidof python)" ] then sudo python /home/pi/lightpaint-FF.py fi
And to modify
/etc/crontab
to include* * * * * root /home/pi/EggbeaterCronJob
The desired behavior is to check every minute to see if any Python instances are running and, if not, start the python script. I have verified that this script works when called from a terminal:
pidof python # returns nothing sudo /home/pi/EggbeaterCronJob pidof python # returns new process ID
But after updating my
crontab
, no python process ID ever appears on its own accord. Just to be sure, I also made sure I wasn't making a boneheaded mistake in mycrontab
configuration:* * * * * root touch test-freaking-cron
This verified that
cron
was indeed responding to my edits.-
jasonwryan almost 11 yearsWhat distro are you running on your Pi?
-
goldilocks almost 11 yearsWhile I guess the cron job should work, it might be more straightforward to just use an infinite loop in the bash script with
sleep 60
at the end. Also makes it easier to stop ;). Very very cool, that LED strip light painting stuff! -
Halfgaar almost 11 yearsAnd then put that in /etc/rc.local
-
user2914606 almost 11 yearswhy don't you write a service file? it depends on what distribution you're using but that seems easier than using cron.
-
nivek almost 11 yearsstrugee, thanks. I was able to get some advice from friends and waded through the thick documentation for update-rc.d. IMHO the task of making sense of the dense SysV documentation is harder than a cron job (for a beginner!) :-) But in terms of functionality, yes, I agree with you that starting the program on startup makes more sense than running a redundant script every minute. (The cron option would have the advantage of auto-restarting the python code if it were to break---but we've already used this toy for several hours at a time with no problem, so hopefully that won't be needed!)
-
nivek almost 11 yearsFor future Googlers, for the record, I did not figure out why the Cron script didn't work.
-