1
votes

I have a python script that has a While True: in it that I would like to have run on startup on a raspberry pi running Jessie.

So far I have a startup bash script in /etc/init.d called startup.sh which contains

sudo python3 /home/pi/Desktop/Scripts/bluez3.py &

When the raspberry pi starts up, the script does run but after 20 minutes the script seems to stop. I have logging in my script and the time-stamp stops exactly 20 mins in.

I did some reading and I think the best option would be to create the python script as a service on the raspberry pi. However, I have not been able to find a decent tutorial about how to do this (and my lack of python knowledge).

My question is, is there another way to resolve my problem or does anyone know of a good tutorial on how to make the python script into a service.

Thanks!

1
Hmm - well why if we see if we can figure out what's killing it? Could you put the contents of that script, here? You'll ultimately wind up with the same result even if you make it a service - you'll just have systemd telling you it stoppedrm-vanda
have you tried running it in foreground in a terminal and witnessing if it fails in 20 minutes with some traceback as stderr?odradek

1 Answers

1
votes

given the name of your script, I'm guessing it's related to some bluetooth stuff. It's likely that after 20 min, whatever you're checking/needing in your script gets unaccessible and throws an exception or something like that. Like a resource being locked, or a bt device being disconnected or a module being unloaded or unavailable or [insert edge case reason here]…

that being said, in between creating a systemd service, you can first play with supervisorctl which is just an apt install supervisor away.

then if you really want to launch it as a service, you can find plenty of examples in /lib/systemd/system/*.service, like the following:

[Unit]
Description=Your service
Wants=
After=bluetooth.target # I guess you need bluetooth initialised first

[Service]
ExecStart=/usr/bin/python3 /home/pi/Desktop/Scripts/bluez3.py
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always

[Install]
WantedBy=multi-user.target

which I customized from the sshd.service file 😉