I am attempting to create a safe reset switch according to these instructions. I have implemented the interrupt-based solution (listing 1) which runs fine if I start the script from the command line, but I am unable to get it to work successfully when run from startup. The article was written prior to the implementation of systemd on the RPi, and the suggested use modifying rc.local is not working for me.

In debugging the command, I have found that the issue is with this portion of the script:

def loop(): 

The function is used to prevent the program from exiting without using a lot of system resources. Trying to start a service based upon a script with this command, however, fails. I am able to use the while loop in listing 2 in a script assigned to a service; however I'd like to avoid the loop if possible in order to minimize resource use.

Questions: (a) Is there a way to properly implement raw_input() in a script to be called by a systemd service? (b) Are there other resource-light methods to prevent a python script from exiting?

  • 1
    I didn't analyse the script, but I run a simpler script. This uses zero resources (other than a bit of memory), as it blocks the process (started by cron) until activated. See raspberrypi.stackexchange.com/a/42945/8697
    – Milliways
    Commented Apr 17, 2017 at 1:29
  • @Milliways thanks for the link to your nice answer. It is similar to the 2nd listing I mention, where GPIO checks are wrapped in a while loop. I'm trying to avoid the need to include sleep(1) in the function, (and learn a bit more about interrupts during the process. Commented Apr 17, 2017 at 1:49

1 Answer 1


The answer to my first question, is there a way to properly implement raw_input() in a systemd service is no. Documentation for systemd states that input is not allowed:

System services are unable to read from the standard input stream. When systemd starts a service, it connects its standard input to /dev/null to prevent any interaction with the user.

I have come up with a very "hacky" workaround that addresses question 2, are there other resource-light methods to prevent a python script from exiting. It involves two steps, the first is to create a temporary file and the second is to tail that file:

os.system('date > /tmp/shutdownswitch')
os.system('tail -f /tmp/shutdownswitch')

As far as I can tell, there is no significant impact on the performance of a RPi-2. I cannot tell the difference in resource usage between this method and the use of a while loop; the added advantage of my solution being that the response time for a button press is better. (Recall, if the user presses the button while the sleep function is called, it will not be registered.)

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.