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