So I have a python script that starts as expected during power-on by following: Startup by systemd

My Pi has a button connect to GPIO which is used to stop the logger and then shut down the Pi like this:

     print('Shutting down!!!')
     with open('debug_file', 'w+') as f:
        f.write('Starting shutdown!!\n')
     with open ('failed_shutdown', 'w+') as f:
        f.write('Shutdown from script failed\d')

So on a normal power-on and then pressing the button my script writes to the 'debug_file' and it does not write to the 'failed_shutdown' file but the pi is not shutdown since I can still ssh into it.

When starting the script from console and then pressing the button it does the same thing but also shuts down the pi.

How do I debug this? It feels like there is a different context when starting the script as a service from systemd then when starting from console. I have followed the above link exactly except for names and user.

1 Answer 1


Now I feel stupid,

The solution was to not use the copied from another link:


but insted directly use:

os.system("sudo shutdown -h now")
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Apr 3, 2020 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.