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

    ensuring ES gracefully finish and save metadata in every system shutdown

    Scheduled Pinned Locked Moved Help and Support
    shutdown scriptemulationstatiofavoriteslast playedmetadata issues
    96 Posts 26 Posters 37.0k 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.
    • cyperghostC
      cyperghost @Yahmez
      last edited by

      @yahmez Can you please provide your shutdown script? Maybe this gives the kick ;)
      @meleu I'm hunting down that rabbit - it's name is BASH

      YahmezY 1 Reply Last reply Reply Quote 0
      • YahmezY
        Yahmez @cyperghost
        last edited by

        @cyperghost
        I thought this would work for any shutdown script? 😜
        Here it is: https://pastebin.com/25GKw9xv

        meleuM cyperghostC 2 Replies Last reply Reply Quote 0
        • YahmezY
          Yahmez @meleu
          last edited by Yahmez

          @meleu said in ensuring ES gracefully finish and save metadata in every system shutdown:

          chmod a+x /path/to/killes.sh

          Thank you, this got the script working. Only problem is the case LED remains faintly lit. I cannot figure this out... I mean everything shuts down, the fan turns off, but the LED is still getting a little bit of power. Somehow by using the killes script the mosfet switch stays on. The only thing that can keep it on after the power button is is turned off is a pi GPIO staying high. But then again it's not fully on because the pi and fan are off... it's puzzling. I added a GPIO.cleanup() line to the end of my script but it did not seem to matter.

          1 Reply Last reply Reply Quote 0
          • YahmezY
            Yahmez
            last edited by

            After 5 minutes the LED fully extinguishes, so I guess this is a non issue even though it perplexes me. Thank you @meleu for working coming up with this graceful exit to ES. I really appreciate it.

            1 Reply Last reply Reply Quote 1
            • meleuM
              meleu @Yahmez
              last edited by

              @yahmez said in ensuring ES gracefully finish and save metadata in every system shutdown:

              @cyperghost
              I thought this would work for any shutdown script? 😜

              Yes. This is the intention of my approach here. ;)

              I'm glad to know it's now working for you!

              Have a nice weekend.

              • Useful topics
              • joystick-selection tool
              • rpie-art tool
              • achievements I made
              1 Reply Last reply Reply Quote 0
              • cyperghostC
                cyperghost @Yahmez
                last edited by

                @yahmez Yes the solution from @meleu is working in general (thanks to him!)

                ... but I just want to take a look how the mass of these scripts are written. I'm currently working on annother way to shutdown and to give the user more control about the Pie.

                One disadvantage will be that the shutdown scripts (bash, python or any other language) needs to be modified a bit. On the other hand, there will be no need to add something manually to systemd.

                I don't know which method is better and I don't want to judge about. In the end the user decides on his own. The way how this script was born can be read on the first posting. And we can thank @meleu for his long breath and brillance in improve such scripts.

                For me: It is always better to have two working ways ;)
                Thanks for your help...

                1 Reply Last reply Reply Quote 1
                • meleuM
                  meleu
                  last edited by meleu

                  @BuZz do you think it would be valuable to add the trick described in the OP as an option in emulationstation.sh script module?

                  Summing up what it is: a way to ensure that ES will cleanly exit saving all metadata right before any shutdown/reboot.

                  If yes I can submit a PR for your evaluation.

                  EDIT: I'm thinking about an option like "Cleanly exit ES at any shutdown/reboot."

                  • Useful topics
                  • joystick-selection tool
                  • rpie-art tool
                  • achievements I made
                  BuZzB 1 Reply Last reply Reply Quote 0
                  • BuZzB
                    BuZz administrators @meleu
                    last edited by BuZz

                    @meleu It's an issue with 3rd party reboot scripts? Looking briefly at op, I don't see why this is needed in RetroPie (it's not a RetroPie problem).

                    To help us help you - please make sure you read the sticky topics before posting - https://retropie.org.uk/forum/topic/3/read-this-first

                    meleuM 1 Reply Last reply Reply Quote 0
                    • meleuM
                      meleu @BuZz
                      last edited by

                      @buzz said in ensuring ES gracefully finish and save metadata in every system shutdown:

                      @meleu It's an issue with 3rd party reboot scripts?

                      Not only 3rd party reboot scripts, but any shutdown method different than ES quit menu (example: "Perform reboot" on retropie_setup while ES is running, or shutdown -h now or reboot via SSH).

                      Looking briefly at op, I don't see why this is needed in RetroPie

                      Before the "Favorites/Last Played" feature was implemented, the "save metadata on exit" wasn't so important. But now it is. It can be really frustrating to lose your favorites after spending some time curating the list...

                      RetroPie doesn't actually need this, but it would avoid several topics here in the forum with people asking for help on how to not lose Favorites/Last played data when shutting down the pi with power buttons like Mausberry, Powerblock, etc.

                      (it's not a RetroPie problem)

                      Well, I have no intention to label it as a "RetroPie problem", but here is a 100% clean RetroPie use case (no custom configs/hacks involved):

                      1. Boot you raspi.
                      2. In ES add some games to Favorites.
                      3. Launch and exit some games (just to add some entries in the Last Played).
                      4. Launch retropie_setup via RetroPie Menu in ES.
                      5. Perform a reboot via retropie_setup.
                      6. After rebooting check that your Favorites/Last Played wasn't saved.

                      IMHO adding that trick as an option in emulationstation.sh script module has more pros than cons.

                      • Useful topics
                      • joystick-selection tool
                      • rpie-art tool
                      • achievements I made
                      BuZzB 1 Reply Last reply Reply Quote 1
                      • BuZzB
                        BuZz administrators @meleu
                        last edited by

                        @meleu Well my advice would be to not reboot from retropie-setup and use ES. Maybe I should add a warning about rebooting when launched from ES. 3rd party reboot scripts can easily kill and wait for ES first. I'm not keen to invade the init system with ES to handle reboot scenarios outside of ES.

                        To help us help you - please make sure you read the sticky topics before posting - https://retropie.org.uk/forum/topic/3/read-this-first

                        meleuM cyperghostC 2 Replies Last reply Reply Quote 0
                        • meleuM
                          meleu @BuZz
                          last edited by

                          @buzz OK, I got it. Let's leave this as a tinker trick. I'm going to add this topic to the "Useful topics" post.

                          • Useful topics
                          • joystick-selection tool
                          • rpie-art tool
                          • achievements I made
                          1 Reply Last reply Reply Quote 1
                          • cyperghostC
                            cyperghost @BuZz
                            last edited by cyperghost

                            @buzz Well you can add a file "es-sysrestart" to folder tmp and send SIGTERM to EmulationStation-binary PID only - then this script provided with ES will takeover the shutdown sequence. If no active ES-PID is available the usual system shutdown can be performed.

                            Of course a warning hint within RetroPie-setup is also considerable and needs no maintainment - just a bit of user brain :)

                            BuZzB 1 Reply Last reply Reply Quote 0
                            • BuZzB
                              BuZz administrators @cyperghost
                              last edited by

                              @cyperghost Thanks. I am aware of that btw, I wrote that script :-)

                              To help us help you - please make sure you read the sticky topics before posting - https://retropie.org.uk/forum/topic/3/read-this-first

                              1 Reply Last reply Reply Quote 0
                              • AphexA
                                Aphex
                                last edited by Aphex

                                Being a owner of a NESPi Case (and Pi 3 model B), can i follow OP's guide as it is right now? or is there something else i should do/know about to make it work?
                                I've been trying to follow the whole thing but as a lesser-experienced linux user i still feel sorry to ask :s
                                EDIT: So sorry, i meant a NESPi Case, not RasPi

                                meleuM 1 Reply Last reply Reply Quote 0
                                • meleuM
                                  meleu @Aphex
                                  last edited by

                                  @aphex said in ensuring ES gracefully finish and save metadata in every system shutdown:

                                  Being a owner of a RasPi Case (and Pi 3 model B), can i follow OP's guide as it is right now?

                                  Yes. Let me know if you face some problem.

                                  • Useful topics
                                  • joystick-selection tool
                                  • rpie-art tool
                                  • achievements I made
                                  AphexA 1 Reply Last reply Reply Quote 0
                                  • AphexA
                                    Aphex @meleu
                                    last edited by

                                    @meleu After trying the guide with my NesPi case, i get the same initial result that @yahmez got. It's not saving changes and i also get the LED remains faintly lit (though i don't mind if it's fully off after 5 minutes).

                                    @yahmez Have you performed extra modifications or anything to make it work? You posted this link ( https://pastebin.com/25GKw9xv ) but i am unsure of what to make of it.

                                    YahmezY 1 Reply Last reply Reply Quote 0
                                    • YahmezY
                                      Yahmez @Aphex
                                      last edited by Yahmez

                                      @aphex The only thing I did was make the killes.service script executable. The code you linked is the shutdown script I am using. Although I am assuming you made the modifications to the NESPi as outlined in my PDF...

                                      meleuM 1 Reply Last reply Reply Quote 0
                                      • meleuM
                                        meleu @Yahmez
                                        last edited by

                                        @yahmez said in ensuring ES gracefully finish and save metadata in every system shutdown:

                                        The only thing I did was make the killes.service script executable

                                        The script is killes.sh, and, yes, it must be executable. There's nothing wrong with the .service file. ;-)

                                        • Useful topics
                                        • joystick-selection tool
                                        • rpie-art tool
                                        • achievements I made
                                        YahmezY 1 Reply Last reply Reply Quote 0
                                        • YahmezY
                                          Yahmez @meleu
                                          last edited by

                                          @meleu
                                          Ha, sorry. My error.

                                          1 Reply Last reply Reply Quote 0
                                          • cyperghostC
                                            cyperghost
                                            last edited by cyperghost

                                            @meleu As I mentioned in my latest script!
                                            To trigger ES events we can use incron job.

                                            you can set it up as I done in my github script and maybe you can add a new user (afaik you are not using user PI on your system)

                                            Then there can be two ways! To locate in which status ES shutdown now we have to use a kind of logger as incron just writes events to syslog....

                                            So just write echo $# in incronjob results in an entry of syslog...

                                            So we would write this to icrontab... maybe sudo?? sudo /etc/log.sh $#

                                            /tmp IN_CREATE /etc/log.sh $#
                                            

                                            log.shwill contain the redirection from incron

                                            #!/bin/bash
                                            # incron-logger to determine EmulationStation-quit-status
                                                esstatus=("es-restart" "es-sysrestart" "es-shutdown")
                                            
                                                for status in "${esstatus[@]}"; do
                                                           [[ "$status" == "$1" ]] && echo "$1" > /etc/esstatus.log
                                                done
                                            

                                            Important! That needs a bit of fine tuning! Because every file written will be logged! Unix uses tons of temporary files ;)
                                            FIXED!


                                            EDIT:

                                            /tmp IN_CREATE cp $@$# /dev/shm/
                                            

                                            would be much nicer ... then presence of ES status files have just to be tested ;) As these files are zero files, there is no chance to break copy process. And /dev/shm is "self cleaning" after shutdown ;)


                                            Then we need to modify @meleu s script to determine the generated logfile and with this we can generate action based on ES-quit-status ;)

                                            The solution I presnted yesterday lacks of speed, it works on a Raspberry 3 but maybe if the RPi 4 is available the shutdown is performed so fast my script will just terminated without action!

                                            So I think systemd gots a more bright future.

                                            @meleu
                                            It's your turn now ;)
                                            I hope that this gives you encourage to modify your script. Me and lot's of users will appreciate this. As I did not care about user rights managment, there maybe must be made sudo calls from time to time.

                                            The king way would be to integrate the logger into the killes.sh and I vote for a rename of your script.... it sounds so destructive ;)

                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              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.