0

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:

  if DEBUG_FLAG:
     print('Shutting down!!!')
     with open('debug_file', 'w+') as f:
        f.write('Starting shutdown!!\n')
  try:
     subprocess.Popen(['sudo','shutdown','-h','now'])
  except:
     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.

0

Now I feel stupid,

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

subprocess.Popen(['sudo','shutdown','-h','now'])

but insted directly use:

os.system("sudo shutdown -h now")
1
  • 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 '20 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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