Search code examples
python-3.xraspberry-pitmuxraspbian-stretch

Python program crashes when running in tmux session from boot


I have a python3 program that I run without any problems on a headless raspberry pi configuration where I connect using ssh. I logon to my pi, start a tmux session, and run the program. It will run indefinitely. During development when an exception would occur I the python program would terminate and I would see the error info.

Now if my pi were to reboot while I wasn't around I would want it to run this program automatically. I implemented the following steps to make this happen:

I created a script which contains this line:

tmux new-session -d -s xbnw 'python3 /home/pi/python/XbNWSer05.py'

Then I modified /etc/rc.local to include this line:

sudo -u pi bash /home/pi/tmux_xbnw.sh

Now when I reboot my pi everything works for a few minutes. The program is clearly running. I can logon and attach to the session and see my debug output and everything looks fine.

After a few minutes though something goes wrong and if I'm logged on and connected to the tmux session I suddenly find myself at a command prompt as though the program never ran. Also the tmux session is no longer active. There's no indication what happened.

1) Am I starting my program improperly? 2) If so is there a way to figure out what happened?


Solution

  • Add a logfile, either hacky by hand or with the logger module. Then you can find out if the programm started and you can log every minute or so to see when it dies and what was the last state.

    You can also check the syslog whether there was a reboot. I had a bad power supply cable which made my Pi reboot in past...