• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
RetroPie forum home
  • Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

Modify a safe shutdown script.

Scheduled Pinned Locked Moved Help and Support
shutdown scriptshutdownmenu
20 Posts 5 Posters 5.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S
    SixSpeedDeath
    last edited by 10 Apr 2018, 18:56

    When tinkering with a Super Kuma 9000 case, and shutting down, I noticed that occasionally the *.xml corrupts, so I peeked at the script, which kills the emulationstation process and then shuts the Pi down. Same with RetroFlag's new safe shutdown script.

    Can someone point me to the code emulation station runs when you select shut down from the menus, so I can try to incorporate that into the safe shutdown script, hopefully preventing xml corruption.

    M 1 Reply Last reply 10 Apr 2018, 19:35 Reply Quote 0
    • M
      mediamogul Global Moderator @SixSpeedDeath
      last edited by mediamogul 4 Oct 2018, 21:05 10 Apr 2018, 19:35

      @sixspeeddeath

      I modified the ES kill portion of this script for my own needs and it's been working out very well for months now. An isolated safe shutdown based on my own would look something like:

      #!/bin/bash
      ESPID="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")"
      if [ -n "$ESPID" ]; then
      kill "$ESPID"
      while s=$(ps -p "$ESPID" -o s=) && [ "$s" ] && [ "$s" != 'Z' ]; do
      sleep 1
      done
      fi
      sleep 1
      sudo shutdown -h now

      RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

      S C 2 Replies Last reply 10 Apr 2018, 19:56 Reply Quote 1
      • S
        SixSpeedDeath @mediamogul
        last edited by 10 Apr 2018, 19:56

        @mediamogul since I'm still a novice at this, will that allow emulationstation to finish writing whatever it needs to before shutting down?

        M 1 Reply Last reply 10 Apr 2018, 20:05 Reply Quote 0
        • M
          mediamogul Global Moderator @SixSpeedDeath
          last edited by 10 Apr 2018, 20:05

          @sixspeeddeath

          It will indeed. I've been using it for months. One thing to note is that if you're going to be calling the script from a menu within Emulation Station itself, you'll want to call the script from another script. Meaning that if the script above was named killes.sh and it was located in your home folder, you would call it from another script accessible from an Emulation Station menu using something as simple as:

          #!/bin/bash
          bash /home/pi/killes.sh &

          RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

          S 1 Reply Last reply 10 Apr 2018, 20:24 Reply Quote 1
          • S
            SixSpeedDeath @mediamogul
            last edited by 10 Apr 2018, 20:24

            @mediamogul I was going to add it to the shutdown script located in "https://packagecloud.io/kintaro/pcb/packages/raspbian/jessie/kintarosnes_2.4.0_armhf.deb".

            It looks like the install runs it bases on GPIO inputs toggled by the switches.

            Are you saying I'd need to modify the script to toggle the menu item in emulationstation itself? Or that if emulationstation wanted to shutdown, it would need to access the new shutdown script?

            M 1 Reply Last reply 10 Apr 2018, 20:39 Reply Quote 0
            • M
              mediamogul Global Moderator @SixSpeedDeath
              last edited by mediamogul 4 Oct 2018, 21:40 10 Apr 2018, 20:39

              @sixspeeddeath

              It looks like the install runs it bases on GPIO inputs toggled by the switches.

              Given your use case, you should only need the first script. I needed the combo, as I launch the script from the Kodi menu of Emulation Station, where I use it to shutdown ES safely and dual boot into OSMC.

              RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

              1 Reply Last reply Reply Quote 1
              • C
                cyperghost @mediamogul
                last edited by 10 Apr 2018, 20:58

                @mediamogul said in Modify a safe shutdown script.:

                @sixspeeddeath

                I modified the ES kill portion of this script for my own needs and it's been working out very well for months now. An isolated safe shutdown based on my own would look something like:

                I would add following line before the kill command
                touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown

                and then remove the sudo shutdown...

                why?
                Because ES startup scripts checks for presence of es-shutdown file and will initiate a shutdown itself.

                If you use es-sysrestart it will reboot
                use es-restart will restart ES

                See this posting here

                M S 2 Replies Last reply 10 Apr 2018, 21:07 Reply Quote 2
                • M
                  mediamogul Global Moderator @cyperghost
                  last edited by 10 Apr 2018, 21:07

                  @cyperghost

                  Very good to know. I'll add it to my own. Thanks.

                  RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

                  1 Reply Last reply Reply Quote 1
                  • S
                    SixSpeedDeath @cyperghost
                    last edited by 10 Apr 2018, 21:13

                    @cyperghost I'll apologize in advance because I'm just starting to learn Linux, so I feel like a middle school kit at Harvard.

                    Does that disable the Reboot and shutdown menu's in Emulation station itself?

                    Is it possible to run es-shutdown from an external script, in essence shutting down emulation station in the same way that emulation station shuts down the pi?

                    My guess would be to replace the "sudo shutdown -h now" with "./path-to/es-shutdown"?

                    If I understand, and removing es-shutdown prevents someone from exiting emulationstation from the menu, that may come in handy for my arcade, but I wasn't planning on disabling it for now.

                    My goal is to dissect their script so I understand it better, and possibly modify it to shut down the RetroPie more safely.

                    C 1 Reply Last reply 10 Apr 2018, 21:22 Reply Quote 1
                    • C
                      cyperghost @SixSpeedDeath
                      last edited by cyperghost 4 Oct 2018, 22:32 10 Apr 2018, 21:22

                      @sixspeeddeath said in Modify a safe shutdown script.:

                      Does that disable the Reboot and shutdown menu's in Emulation station itself?

                      No ... it doesn't touch anything on the Graphic-Shutdown-menu

                      Is it possible to run es-shutdown from an external script, in essence shutting down emulation station in the same way that emulation station shuts down the pi?

                      Yes you can run the script from a external script. You can also modify the script and can path parameters to it.... like ./es_action.sh shutdown to shutdown the Pie
                      or ./es_action reboot to Reboot the Pie or just ./es_action quites to terminate ES and go back to terminal.

                      My guess would be to replace the "sudo shutdown -h now" with "./path-to/es-shutdown"?

                      No just erase that line. ES itself gots a little script that watches for action according to files placed in temp-folder. With touch you set a access date to a file. If the file is not available it will be created. It's better then to redirect like echo > /tmp/es_shutdown

                      If you start the script with /path/to/es_shutdown.sh the Pie will shutdown with sudo poweroffcommand hence to the explaination here

                      My goal is to dissect their script so I understand it better, and possibly modify it to shut down the RetroPie more safely.

                      You can play arround with the script and learn some bash scripting. It isn't so difficult. Don't worry! And welcome to RetroPie

                      S 1 Reply Last reply 11 Apr 2018, 20:50 Reply Quote 1
                      • S
                        SixSpeedDeath @cyperghost
                        last edited by 11 Apr 2018, 20:50

                        @cyperghost @mediamogul, I REALLY want to thank you for all your help.

                        I'm trying to change careers to Linux admin, and the RetroPie has been my "Carrot" for learning the OS.

                        Gradually, I'm picking the stuff up, but responses with information like what is supplied here do more than help me get a toy working, it's something I plan to use for the rest of my life.

                        I can only Bury my nose in a CompTIA book for so long before I need some hands on, and this is exactly what I needed.

                        Thank you.

                        M 1 Reply Last reply 11 Apr 2018, 20:52 Reply Quote 1
                        • M
                          mediamogul Global Moderator @SixSpeedDeath
                          last edited by 11 Apr 2018, 20:52

                          @sixspeeddeath

                          Glad it worked out for you.

                          RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

                          D 1 Reply Last reply 14 Apr 2018, 18:07 Reply Quote 1
                          • D
                            Drakaen391
                            last edited by 14 Apr 2018, 17:59

                            So, I love Python over Bash...

                            but I cant convert one over the other....

                            Any Ideas how to convert this to python?

                            RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
                            Retropie (Latest Stable)
                            PiBox with Wind Tunnel Cooling System

                            1 Reply Last reply Reply Quote 0
                            • D
                              Drakaen391 @mediamogul
                              last edited by 14 Apr 2018, 18:07

                              @mediamogul I am using the original safe shutdown part...

                              import RPi.GPIO as GPIO
                              import os
                              import time
                              
                              
                              ###look at the GPIO for the Button Switch to be depressed
                              
                              GPIO.setmode(GPIO.BCM)
                              GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
                              GPIO.wait_for_edge(3, GPIO.FALLING)
                              
                              def main():
                                  while True:
                                      es-close()
                              
                              def es-close():
                                  os.system("/etc/killes.sh")
                              
                              while True:
                                  print((es-close))
                                  time.sleep(1)
                                  os.system("shutdown now")
                              

                              and a very slightly modded es shutdown code

                              #!/bin/bash
                              ##############################
                              #This Script was created by #
                              #meleu on the retropieforums.#
                              ##############################
                              # Check if EmulationStation is running. Finish the script if doesn't.
                              espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")" || exit 0
                              # the "sed" command below isn't a crypted message :), it's just a trick to
                              # make $emucall regex-safe to use in the "pgrep -f" below.
                              emucall="$(sed '4!d; s/\([\\"]\|[[:alnum:]_]\+=[^ ]* \)//g; s/[][(){}^$*.|+? ]/\\&/g' /dev/shm/runcommand.info)"
                              # If there's an emulator running, we need to kill it and go back to ES
                              if [[ -n "$emucall" ]]; then
                              emupid="$(pgrep -f "$emucall" | tr '\n' ' ')"
                              pkill -P "$(echo $emupid | tr ' ' ',')"
                              kill "$emupid"
                              wait "$emupid"
                              sleep 0.5
                              fi
                              kill "$espid"
                              wait "$espid"
                              sleep 0.5

                              RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
                              Retropie (Latest Stable)
                              PiBox with Wind Tunnel Cooling System

                              C 1 Reply Last reply 18 Apr 2018, 19:50 Reply Quote 0
                              • Q
                                Quackwalks
                                last edited by 16 Apr 2018, 23:30

                                I'm not understanding how or where to fix the problem of the kintaro 9000 case not saving metadata.

                                Pi Model or other hardware: Pi 3b
                                Power Supply used: 5.1v 2.5a
                                RetroPie Version Used: 4.3.17
                                Built From: Pre made SD Image on RetroPie website, updated to 4.3.17
                                USB Devices connected: dongle for wireless keyboard
                                Controller used: Official PS3 Controller

                                D 1 Reply Last reply 17 Apr 2018, 01:32 Reply Quote 0
                                • D
                                  Drakaen391 @Quackwalks
                                  last edited by 17 Apr 2018, 01:32

                                  @quackwalks Do you by chance have a large number of roms scraped?

                                  RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
                                  Retropie (Latest Stable)
                                  PiBox with Wind Tunnel Cooling System

                                  Q 1 Reply Last reply 17 Apr 2018, 02:13 Reply Quote 0
                                  • Q
                                    Quackwalks @Drakaen391
                                    last edited by 17 Apr 2018, 02:13

                                    @drakaen391 I do have a large number of roms scraped.

                                    D 1 Reply Last reply 17 Apr 2018, 02:45 Reply Quote 0
                                    • D
                                      Drakaen391 @Quackwalks
                                      last edited by 17 Apr 2018, 02:45

                                      @quackwalks that could be the issue, increase the wait time a few seconds because emulationstation takes more time to save the meta data if there are alot of scrapped roms

                                      RPi B & RPi 3B OC (Now Raspberry pi 4b 8gb)
                                      Retropie (Latest Stable)
                                      PiBox with Wind Tunnel Cooling System

                                      M 1 Reply Last reply 17 Apr 2018, 03:41 Reply Quote 0
                                      • M
                                        mediamogul Global Moderator @Drakaen391
                                        last edited by 17 Apr 2018, 03:41

                                        @drakaen391 said in Modify a safe shutdown script.:

                                        increase the wait time a few seconds

                                        A static wait time really isn't a good idea for something like this. If you use something similar to the while statement in my example above, it will always take as as much time as it needs, no matter what the circumstances.

                                        RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

                                        1 Reply Last reply Reply Quote 1
                                        • C
                                          cyperghost @Drakaen391
                                          last edited by 18 Apr 2018, 19:50

                                          @drakaen391 About your posted codings.... Does the wait command works in your case? @mediamogul and his while-loop by monitoring PID status is imho better ...

                                          wait can only be used on child processes out of the own subshell

                                          1 Reply Last reply Reply Quote 0
                                          20 out of 20
                                          • First post
                                            20/20
                                            Last post

                                          Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                                          Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.

                                            This community forum collects and processes your personal information.
                                            consent.not_received