Unicorn Hat python script stops after 8+ hours

Hi all,

I’m running a fresh install of Rasbian Stretch on a raspberry3 and running the example flame python file on the Unicorn Hat. It looks lovely, but after I come back into the studio every morning, I find the animation has paused and I have to rerun the script to get it going again.

Note: I’m using the official raspberryPi power cable.

Any advice to ensure it runs continuously for long periods of time?

Cheerio,
Antony

Do you see any debug output from the Python script when it’s stopped?

Has the script edited, or is it still running?

The first port of call would be to find out why it’s crashing- if you’re launching the script headlessly you need to pipe its output to a log file, like so:

python myscript.py > logfile.txt 2>&1

This redirects both the STDOUT (standard output) and STDERR (standard error output) of the Python script to the file logfile.txt. Hopefully when it crashes you’ll see some useful information here.

No debug info was shown.

The script is unedited. I’ve tried running a few of the examples and get the same issue with them.

Amazing! Thanks. I’ll set the script to run using the log statement suggested and post back the contents when it next crashes.

I think he meant, “Has the script exited, or is it still running?”

Will have to make note of the "python myscript.py > logfile.txt 2>&1"
I have several headless setups and that could come in handy some day, nice tip.

After a little research, if it’s indeed a Unicorn HAT and not Unicorn HAT HD (they work differently) then it could be this issue: https://github.com/jgarff/rpi_ws281x/issues/196 which was fixed in https://github.com/jgarff/rpi_ws281x/pull/197

Looking at my order, it’s a Unicorn Hat HD

Okay that rules out that hypothesis.

It wont be an issue with the GPIO pins or the hardware, since the library would happily run without the HAT even attached.

That leaves… I’m not sure! Possibly overheating/undervoltage leading to glitches, or maybe some other script running and interrupting it.

ok, hmm, thanks for the super quick responses @gadgetoid though. I’ll await to see the logfile fill with details and will report back (probably tomorrow morning).

I have to admit, I’m still a relative newbie when it comes to the terminal and python, but absolutely loving it. On the temperature side, I guess I could run a script such as https://projects.raspberrypi.org/en/projects/temperature-log and see if there’s any correlation.

hmm, the animation stopped after roughly 6 hours. The last and only message on the ssh terminal was the following:

packet_write_wait: Connection to fe80::ae3c:198a:c35b:399f%en0 port 22: Broken pipe

but this may have been due to just closing my laptop.

When I checked the logfile, there was nothing in it. The mystery continues. Any further thoughts would be much appreciated.

How are you starting the script?

within the following directory: ~/Pimoroni/unicornhathd/examples

python candle.py > logfile.txt 2>&1

Aha, if you’re starting it like that via an SSH session it will still be attached to your controlling terminal, so when the SSH session dies, the application goes with it.

You should read about screen and how it can be used to keep things like this running between sessions: https://www.howtoforge.com/linux_screen

Although probably what you really want is a systemd unit to start up your Python script. Something like:

[Unit]
Description=My Python Service
After=multiuser.target
Requires=local-fs.target

[Service]
Restart=always
Type=simple
ExecStart=/usr/bin/python /home/pi/Pimoroni/unicornhathd/examples/candle.py

[Install]
WantedBy=default.target

Save this as a file by running: sudo nano /etc/systemd/system/mypython.service and pasting in the above.

Then reload systemd:

sudo systemctl daemon-reload

And start your new service:

sudo systemctl start mypython

You can also get its status:

sudo systemctl status mypython

And stop it:

sudo systemctl stop mypython

And enable it to auto-start on boot:

sudo systemctl enable mypython
2 Likes

Amazing amazing reply @gadgetoid! :D Although, Im still slightly scratching my head as to why my unrelated node.js scripts don’t fall over when my ssh terminal closes.

I’ve just got round to trying this and it all works a charm! Amazing. I tested terminating the shh connection and the flame immediately stopped. Sorry that I didn’t think of this before.

On the plus side, I love the mini start-up script tutorial you’ve posted. So so helpful! Bookmarked and faved.

You’re welcome- I need to whip up a more detailed tutorial on systemd scripts, there’s an awful lot involved in them that, for stuff like firing up a Python script, you can totally ignore. A few boiler plate examples have got me by pretty well for all but the most obtuse of scripts/dependencies.