Python script for shutting down the Raspberry Pi after a certain time
JToivonen last edited by
I have a Raspberry Pi 2 Model B with RetroPie 4.4 installed. I have also installed the latest versions of Python and Python3. My RPi is always plugged to the PSU so that's why I installed a momentary switch to RPI's GPIO pins to start the RPi from STANDBY mode when I want to use it. I use a python script for that.
The only problem in this setup is that if there's a power blackout and when the power comes back on my RPi will power up. I would like to know if anyone knows is there a python script or if anyone can help me to write a python script that would shut down my RPi after a certain time.
Because I use my RPi to play old games the python script should check certain parameter before it will execute the final shut down command. That parameter is (and please if anyone can tell me that I need more parameters tell me about it)...
How much time has passed since I pressed the button or joystick (I use Xbox One controller with USB cable). I don't know if the script will need a countdown timer or something which will reset every time when I press a button or joystick.
If I asked my question unclearly, please let me know and I'll try to explain it more clearly. Thank you for everyone's help in advance!
Ramez last edited by
@JToivonen got what you mean and here is the "un modified" solution...simply if you're playing plug the usb controller and when you finish just unplug it and it will shutdown automatically!, even when a power trigger happens "electricity went off then come back again" and found that your gamepad isn't connected it will shut down :)
here is the python file you just need to edit the line that kills qjoypad to kill whole system add
shutdown -has you use in your momentry switch
don't forget to make the file run at start and special thanks to pablo
idea behind the script is that system creates a js0 file for controller 1 inputs and js1 for controller 2 when connected and so on, the script searches for that file if it's found that mean you've connected the gamepad and probably playing a game and when you finish unplugging the gamepad removes js0 file and when the script discovers the file is not found it kills an app process called qjoypad which you'll change to shutdown
please note you're not gonna use //User Variables section in this code so just ignore it.