Kaki Kaki - 5 months ago 13
Linux Question

Cannot execute python script on Rasberry Pi startup

I'm having problems with starting a python script on Raspberry Pi boot. I have read many threads and tried some tricks, however, none of them worked for me.

The file I am trying to execute is named test.py, it just logs a time to another file, when was Pi's startup:

#!/usr/bin/python
import time

f=open('logger.txt','w')
tim=time.strftime("%H:%M:%S")
f.write('Startup on: %s\n'%(tim))
f.close()


It is located in: /home/pi and I modified the privileges to all (777). I tried to add a line to /etc/rc.local file before exit 0, my rc.local looks like that:

python /home/pi/test.py &
exit 0


Nothing happens on the startup. If I write a .sh file with the same function and change the line in rc.local accordingly, everything works fine.

Could anyone please help me, what is that different in running python script on startup? Thank you, Kaki

Answer

If you don't specify absolute path, open assume relative path to current working directory.

You'd better to try use absolute path first, before you know where the working directory is.

f = open('/home/pi/logger.txt', 'w')