Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 my crontab configuration:

* * * * * root touch test-freaking-cron

This verified that cron was indeed responding to my edits.

share|improve this question
1  
What distro are you running on your Pi? –  jasonwryan Jul 15 '13 at 18:28
    
While 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! –  goldilocks Jul 15 '13 at 18:33
1  
And then put that in /etc/rc.local –  Halfgaar Jul 15 '13 at 18:34
1  
why don't you write a service file? it depends on what distribution you're using but that seems easier than using cron. –  strugee Jul 15 '13 at 19:51
    
strugee, 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!) –  Kevin Jul 16 '13 at 21:08

2 Answers 2

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

share|improve this answer

If you need to start a program at boot time, the easiest way on most setups is to add a command to /etc/rc.local. If the file exists, add this line to it:

python /path/to/lightpaint-FF.py &

If the file doesn't exist, try creating it with the content

#!/bin/sh
python /path/to/lightpaint-FF.py &
exit 0

A few embedded distributions don't support /etc/rc.local. If the one you're using doesn't, tell us what it is.


If you need to monitor the program and relaunch it if it dies, you can do it through init. How to do it depends on which init your distribution uses.

  • For a traditional init, add a line to /etc/inittab. BusyBox can support inittab but it's an optional feature that needs to be selected at compile time.

    lp:2345:respawn:/usr/in/python /path/to/lightpaint-FF.py
    
  • For upstart, write an upstart job](http://upstart.ubuntu.com/getting-started.html).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.