Startup python script not working

Hi there!
This is my first post here and I hope I can help someone with this thread. :wink:

So I have a python script that makes my audio system turn off after some time. Here it is:

from threading import Thread, Event
from subprocess import call
import datetime
import time
import sys
import RPi.GPIO as GPIO
import spotify

stdout_ = sys.stdout
sys.stdout = open("/home/pi/scripts/auto_off.log", 'a', buffering=0)

print("=================== RESTART ===================")
print("Restart time : " +"%d %b %Y - %X"))
GPIO.setup(7, GPIO.OUT) #This is the pin to which the relay is connected
GPIO.output(7, True)
print("GPIO set up")
timer_minutes = 150     # 5 minutes = 300 seconds / 2 this program checks the status every 2 seconds
open("/home/pi/scripts/minutes.txt", 'w+').write("0\n")     # And writes the elapsed time since the last playing status to a file

	spotify_client = spotify.SpopClient("localhost", 6602)
	print("Error while linking to Spotify")
print("Linked to Spotify")
previous_status = 1

def check():
	global previous_status
	date =
	minutes = int(open("/home/pi/scripts/minutes.txt").readlines()[0].strip())
	spotify_status = spotify_client.status()
	if spotify_status[u'status'] in ('stopped', 'paused'):
		if minutes < timer_minutes:
			open("/home/pi/scripts/minutes.txt", 'w+').write(str(minutes+1))
			if previous_status == 1:
				GPIO.output(7, False) #Spegni
				print("[" + date.strftime("%d %b %Y - %X") + "] ~ Stereo shut down")
				previous_status = 0  
			#	if date.minute >= 0 and date.hour >= 23:
			#		call("halt", shell=False)					
		if previous_status == 0:
			GPIO.output(7, True) #Turn on
			print("[" + date.strftime("%d %b %Y - %X") + "] ~ Stereo turned on")
			previous_status = 1
			if spotify_status[u'status'] == 'playing': #Restarts the song
		open("/home/pi/scripts/minutes.txt", 'w+').write("0\n")

class checkThread(Thread):
	def __init__(self, event):
		self.stopped = event
	def run(self):
		while not self.stopped.wait(2.0):

stopEvent = Event()
thread = checkThread(stopEvent)
print("Thread started")

This code works flawlessly if I launch it from SSH. When I add the following line in /etc/rc.local “python /home/pi/scripts/ &” the program does not start up properly.

I found that my log file normally showed the “==RESTART==” line but there was also an error while trying to connect to spotify (triggered by the try structure). My idea is that while rc.local launches the script, spop isn’t ready to accept requests yet or its service is not running yet.
How can I solve this problem? Is it possible to run the script only after spop is launched?

Soo, any help on this?

What about file right ? (does it need chmox +x ?)
When you use ssh are you pi user or root ?
Is your script at the path you write ? (/home/pi/scripts)
In rc.local you can test with su pi -c python /home/pi/scripts/ &
I’ve got two script launched in rc.local just with

python /home/pi/ &

And the most important thing : this feature is included in Volumio2 !
It’s still a RC, so some bug and enhancement will be corrected / added soon. Keep it up to date with the intagrated updater :wink: