I have a GUI program (Python + Qt) coded for my Raspberry Pi. In order to start the program after startup, I have added the following line:

@/bin/bash /home/pi/start_my_program.sh

into .config/lxsession/LXDE-pi/autostart. This works as intended and starts the program after booting up as well as restarts the program when it occasionally crashes.

The problem is, I have a button in the GUI which should turn the Raspberry Pi off when I want to shut it down. For that, upon pressing this button, my code runs gpio.cleanup() and then call("sudo shutdown -P now", shell=True). At this point, in about 4/5 cases the Raspberry Pi successfully shuts down and all is good.

In approximately 1/5 cases though, my GUI program manages to restart before the Raspberry Pi shuts down, preventing the shutdown from happening. How should I fix this?

(I am open to a different way of auto-(re) starting, different way of shutting down or any other solutions. Thank you for your time.)

  • Could you add a delay before your GUI program starts? – CoderMike Jan 20 at 22:36
  • Are you sure your program successfully executes shutdown in these 1/5th of the cases? I would be surprised that a shutdown could be interrupted by restarting your GUI program. First thing I would try is removing that shell=True which has no good use in your code. – Dmitry Grigoryev Jan 21 at 9:08
  • @CoderMike I could add a delay, but this seems un-systematic and would also delay the automatic run on startup. – erthy Jan 23 at 12:20
  • @DmitryGrigoryev I don't know if it successfully executes. I assumed it did, since the program actually dies before restarting (and my shutdown function only clears the GPIO pins, but doesn't actually make the program exit). But I guess it's possible that the clearing of the GPIO pins causes an exception which makes the program crash before the shutdown command is issued. But how would this only happen in 1/5 of the cases? Do you have any ideas on how to test this in more detail? – erthy Jan 23 at 12:22

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.