1

I can not get my python file to start on startup. I've tried four different methods (below) and they all work in command line but none of them appear to run on start up.

As an example, the file is ~/gpio_soundtest.py and it works

#!/usr/bin/python3

import RPi.GPIO as GPIO
from time import sleep
import pygame

#init sounds
pygame.mixer.pre_init(44100, 16, 2, 4096)
pygame.init()
pygame.mixer.init()

WAV = pygame.mixer.Sound("Music/4AM.wav")
WAV.play()

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while 1:
    if GPIO.input(17) == 0:
         WAV.stop()

I also made an sh file for running it and sudo chmod +x file/name. It is tested and also works:

#!/bin/sh
cd /
cd home/pi/
python3 gpio_soundtest.py
cd /

The first method I tried was sudo crontab -e

....more text up here
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
# the line below did not work though it did create an empty log (no help)
@reboot sh /home/pi/gpio_test_launcher.sh >/home/pi/Logs/cronlog 2>&1
# the line below was a test line.  it didn't work either
@reboot echo "$(date)" >> ~/boot.txt

The next thing I tried was sudo nano /etc/xdg/lxsession/LXDE-pi/autostart. Nothing happened on boot.

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash

#neither worked
@/usr/bin/python /home/pi/gpio_soundtest.py
@/usr/bin/python3 /home/pi/gpio_soundtest.py

The next method was sudo nano /etc/rc.local:

.....
# By default this script does nothing.

#I tried running it sh style, did nothing on start up
/home/pi/gpio_test_launcher.sh &

#I tried running it normally, nothing happens on start up
python3 /home/pi/gpio_soundtest.py &

If I run /etc/rc.local it works fine, but I guess it isn't running on startup?

The last thing I tried was installing daemontools as shown here. It also ran fine as with /service/gpio-test-service/run but does nothing on start up.

What am I doing wrong here? Everything I've put in works great on command line but nothing happens when I unplug and replug my pi in. How do I run a Python script on start up, if none of these work?

P.S. I'm using Pi3 and SSHing in via wifi

1

I can't see anything obviously wrong with your crontab example. I haven't looked at the others.

May I suggest you start by getting a minimal example to work.

The following example works for me.

crontab

@reboot (sleep 10; /home/pi/su.py)

su.py

#!/usr/bin/env python

import time

with open("/tmp/mylog", "a") as f:
   f.write("Hello, I am running at " + time.asctime() + "\n")
   f.close()

Misc

su.py is made executable with

chmod +x su.py

$ cat /tmp/mylog
Hello, I am running at Wed May 18 07:47:48 2016
0

After much work I figured out that the issue was with my audio card and sudo. For whatever reason, any command run from sudo does not output audio, so my solution ended up being to use sudo -u pi python3 gpio_soundtest.py in order to make run the command from user pi -u pi.

After this switch, all of the methods work great.

Of all the methods, I most recommend using deamontools because it will restart the program if it fails.

sudo crontab -e is also a good choice, but there is a difference between it and crontab -e without the sudo. For my case, only the sudo one ran.

I did not retest sudo nano 1/etc/rc.local, but it seemed a little precarious to me.

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart is perhaps the one I trust least of all. I'm pretty sure it doesn't work unless you run the desktop.

Anyways, I hope this helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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