Restart a python script automatically


I am quite new to python although have been using dietpi to run pihole for ages.
I have some scripts I have been able to get hold of to scrape the data from a solar logger. These scripts for some reason sometimes fail and then just end themselves. Is there a way of making this script restart if it fails?

I have managed to add it to the autostart function so it loads with the pi already.

any helpwould be apprecitaed

Hi, you can use supervisord. It can start your python script at startup and restart if it close for a problem.

This would require Home Assistent to be installed. Isn’t it. And I’m not sure the OP already have.


But it’s the same on DietPi.

supervisord is a process control system, independent of Home Assistant, just used as well by Home Assistant (supervised edition, or probably not even that?) or am I missing context? However, instead of installing another process control system, systemd could be used as well:

cat << '_EOF_' > /etc/systemd/system/mypython.service
Description=My Python script

ExecStart=/usr/bin/python3 /path/to/my/


systemctl daemon-reload
systemctl enable --now mypython
systemctl status mypython
1 Like

Hi folks

Thanks for the input.

I do indeed run homeassistant but that runs on its own VM in virtual box on windows.

I have a dietpi install for pihole and solismon3 which I have just managed to get sorted out. It’s the solismon3 script that every now and again decides it wants to quit.
Iv got around it b ly setting it to autorun and some keepalive setting

And then I have a second lighter dietpi install as a mosquitto broker.

This is kind of how I have done it…set it as a service and then told it to autorestart.

Thanks for the input.