RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    Mausberry Shutdown Script Doesn't Save Metadata

    Scheduled Pinned Locked Moved ControlBlock, PowerBlock & Co.
    mausberry
    251 Posts 10 Posters 100.2k 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.
    • hansolo77H
      hansolo77
      last edited by

      How does ES power off? I've never seen it do that before.. I've been able to have it do a restart of ES, a reboot of the Pi, and do a shut down. But when I do the shutdown, it just kills everything running but leaves the power running. The script, provided by Mausberry, tells the circuit everything is off then disconnects the power. As long as I've had the circuit, I've never had it completely cut power when I do a shutdown from within ES.

      Who's Scruffy Looking?

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

        @cyperghost said in Mausberry Shutdown Script Doesn't Save Metadata:

        "/opt/retropie/supplementary/.*/emulationstation([^.]|$)"

        ES Powers Off through it's script emulationstation.sh

        I've done a small rework to your gpio-poweroff script!
        I remarked the changes I made now it should work 100% and you can always use PowerOff button! I've just tested my written part. That worked as I was logged in as user pi - I hope there are no restrictions to file system.

        @meleu Is coding style better now?

        #!/bin/bash
        
        #this is the GPIO pin connected to the lead on switch labeled OUT
        GPIOpin1=23
        
        #this is the GPIO pin connected to the lead on switch labeled IN
        GPIOpin2=24
        
        echo "$GPIOpin1" > /sys/class/gpio/export
        echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
        echo "$GPIOpin2" > /sys/class/gpio/export
        echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
        echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
        while [ 1 = 1 ]; do
        power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
        if [ $power = 0 ]; then
        sleep 1
        else
        
        # End Emulationstation if condition of running binary is true (not tested!)
        # v1 07/21/17 by cyperghost - Inital run 
        # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown
        espid=$( pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)" ) # PID of binary only
        if [ "$espid" ]; then                                                         # Condition: PID is not equal 0 or empty then use ES shutdown
           echo "ES binary is running - I'm using ES internal shutdown"
           touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown                     #v1.1 Change file permission of es-shutdown to user:group pi:pi
           pkill -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)"
           exit
        else
           echo "ES binary is closed - I proceed with usual shutdown"
        fi
        # End Emulationstation if condition of running binary is true (not tested!)
        
        wait
        sudo poweroff
        fi
        done
        1 Reply Last reply Reply Quote 0
        • hansolo77H
          hansolo77
          last edited by hansolo77

          I'm home from work now, so I will test this asap!

          Didn't work. I see in the code you have it doing an echo. That should mean in either case it will display a line indicating which method it is shutting down right? All I'm getting on my screen is:

          rm: cannot remove "/tmp/es-shutdown": Operation not permitted
          

          It does shutudown though, and it looks like it's saving the metadata now. I checked my Last Played before, started a game, checked a game and saw a new "most recent". I then did the shutdown (via the power button). When I booted back up, the "most recent" is now there. Now if I can only figure out how to get that "error" gone. Perhaps a sudu?

          Who's Scruffy Looking?

          cyperghostC 1 Reply Last reply Reply Quote 0
          • hansolo77H
            hansolo77
            last edited by hansolo77

            I'm trying to figure this out on my own.. here's what I've figured out by looking at the scripts...

            The script located at:

            /opt/retropie/supplementary/emulationstation.sh
            

            Is doing nothing more than watching the /tmp/ folder for the creation of either "es-restart", "es_sysrestart", or "es_shutdown". At that point, it erases that file then does either a sudo reboot or a sudo poweroff depending on the file it detects. I don't understand why we would be calling this script from the switch script. I mean, the new additions you give the script seem to me like we're identifying the PID (process ID) of ES, then create the "es-shutdown" file for the other script to initiate the shutdown. Then you have a pkill in there, which would make sense, but it looks like (based on the error I get on the screen) the other script is initiating the shutdown, not the switch script. I'm also not sure why you have the path with a .* in it. Isn't that path redundant?

            opt/retropie/supplementary/.*/emulationstation
            

            I know I'm a total newb to this, but I'm trying to understand...

            Who's Scruffy Looking?

            1 Reply Last reply Reply Quote 0
            • hansolo77H
              hansolo77
              last edited by hansolo77

              OK I went back and re-read your initial suggestion @cyperghost, now that I've explored a little more into this. If there is indeed a way to have ES shut down internally prior to initiating the poweroff, that's the best way to do it. But I don't see a method via the emulationstation.sh script to just exit ES. There are only keys to reboot and shutdown. Am I missing something? It looks like all I need is the command to exit ES.

              Who's Scruffy Looking?

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

                @hansolo77

                rm: cannot remove "/tmp/es-shutdown": Operation not permitted

                Ah... this is caused by your GPIO script seems to be runnig as user "ROOT"
                So the file es-shutdown is also created by user "ROOT" and can't removed by USER "Pi" which the ES-process belongs to ;)

                There is a solution! So I told you:

                cyperghost wrote: That worked as I was logged in as user pi - I hope there are no restrictions to file system.

                Now if I can only figure out how to get that "error" gone. Perhaps a sudu

                Out of the error please add touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown instead of only touch /tmp/es-shutdown. Pleaso don't mess around with sudo. The process of your script runs already under root, so there is no need to sudo again.

                The error you've posted is a permission error caused by user root, now we change the file to permit user pi ;)

                I updated the script posted above, so it should work for all processes now. Can you please test and tell me?

                I see in the code you have it doing an echo. That should mean in either case it will display a line indicating which method it is shutting down right? All I'm getting on my screen is

                That's just for troubleshooting and indicating, you can remove or comment the lines with # if it works as you want. I still left the else branch for you (it's a useless code piece for now), maybe you need it for a future condition.

                I'm also not sure why you have the path with a .* in it. Isn't that path redundant?
                opt/retropie/supplementary/.*/emulationstation
                I know I'm a total newb to this, but I'm trying to understand...

                That's for indicating the binary process and other emulationstations-binaries like emulationstation_kidsmode, emulationstation_powersaver.... without the * you would just kill the ordinary binary emulationstation not more not less. @Buzz and @meleu did a nice job in creating this script. I also appreciate the help of some guys here in the forum so there is no intentention te be called a noob - rookie sounds better :)

                here are only keys to reboot and shutdown. Am I missing something? It looks like all I need is the command to exit ES.

                To exit ES: It's an internal c++ command. I never needed to exit ES becasue I always log in via SSH if I need a terminal. So I can't give correct answer here how ES can be proper quitted - maybe via killall emulationstation?

                As I told you... use the chwon command to change user rights of created file and I've updated the bash script and commented it.


                EDIT - the script should work so far but there is space for doing some code optimizings:

                • I think you can remove waitcommand you initiated because I made an exit and give shutdown control back to ES (but only if binary PID was detected! If the ES binary is not detected the GPIO script runs as usual)
                • If you want you can remove complete else branch (don't remove fi command!)
                • you can remove or comment my two echo commands to give if-condition out to screen - it can be used for bug hunting
                1 Reply Last reply Reply Quote 0
                • hansolo77H
                  hansolo77
                  last edited by

                  I almost posted a big grin, until I restarted... The script now displays just one word on the screen when I hit the power button:

                  Terminated
                  

                  But when the system came back up, my Last Played collection hadn't saved the most recent game I played. So I think we're back to square one again. :(

                  Who's Scruffy Looking?

                  pjftP 1 Reply Last reply Reply Quote 0
                  • pjftP
                    pjft @hansolo77
                    last edited by

                    @hansolo77 Just to make sure we're looking at the right part of the problem, what's the result of running ls -lon the folder of the gamelist that should contain the game you last played?

                    Does the date and time of the file match the time when you rebooted? What's the current date and time of that file?

                    1 Reply Last reply Reply Quote 0
                    • hansolo77H
                      hansolo77
                      last edited by

                      The date and time didn't update... I just played a game right before testing 10 minutes ago, I see this:

                      pi@retropie:~ $ ls -l /opt/retropie/configs/all/emulationstation/gamelists/megadrive/
                      total 1560
                      -rw-r--r-- 1 pi pi 1594754 Jul 21 01:26 gamelist.xml
                      

                      Who's Scruffy Looking?

                      1 Reply Last reply Reply Quote 0
                      • pjftP
                        pjft
                        last edited by

                        Got it.

                        So we're looking at the right end of the problem: it's not saving the metadata.

                        Also, do you have "Save Metadata on Exit" turned on?

                        Just wanted to check you're not chasing something from the wrong end.

                        1 Reply Last reply Reply Quote 0
                        • hansolo77H
                          hansolo77
                          last edited by hansolo77

                          Yeah.. I've never changed it. Just double checked too... I did, however, launch the game via the Favorites collection rather than from the actual system menu.. gonna test that now.

                          EDIT - Seems like it liked it ok when I ran it from the system menu.. Gonna further test..

                          Who's Scruffy Looking?

                          1 Reply Last reply Reply Quote 0
                          • pjftP
                            pjft
                            last edited by

                            That shouldn't have any effect, to be honest.

                            1 Reply Last reply Reply Quote 0
                            • hansolo77H
                              hansolo77
                              last edited by

                              LOL WTF...

                              Now it's working. I tested a different system from my Favorites, exited back to ES then powered off. That most recent game is now at the top of the Last Played list. Go figure. Maybe it just need to go through a complete "reboot" cycle after editing the script? I've also noticed that the Terminated indicator on the screen has gone away.

                              Who's Scruffy Looking?

                              1 Reply Last reply Reply Quote 0
                              • pjftP
                                pjft
                                last edited by

                                I'm thinking that whatever solution you have going on there at the moment is not reliable enough. :)

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

                                  @pjft I think there was a problem with file permission.
                                  But that should be solved via chmod.

                                  @hansolo77 Can the topic marked as solved? Are there any issues? Can you please post your GPIO script?

                                  1 Reply Last reply Reply Quote 0
                                  • hansolo77H
                                    hansolo77
                                    last edited by hansolo77

                                    I don't want to mark it as solved until I've done further testing. I'd like to see if it still saves if I'm playing a game and shut it down before returning to ES (probably won't, so the script might need updated). I also want to test to see if it saves after I play a game then let the screensaver run for a bit and shutdown during the screensaver. For now, the testing via direct system menu and via the Favorites menu seems to be working. I'm running out for a few hours so I won't be able to test again for some time. But fingers are crossed.

                                    Here's the script as it is right now:

                                    /etc/switch.sh

                                    #!/bin/bash
                                    
                                    #this is the GPIO pin connected to the lead on switch labeled OUT
                                    GPIOpin1=23
                                    
                                    #this is the GPIO pin connected to the lead on switch labeled IN
                                    GPIOpin2=24
                                    
                                    echo "$GPIOpin1" > /sys/class/gpio/export
                                    echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
                                    echo "$GPIOpin2" > /sys/class/gpio/export
                                    echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
                                    echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
                                    while [ 1 = 1 ]; do
                                    power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
                                    if [ $power = 0 ]; then
                                    sleep 1
                                    else
                                    
                                    # End Emulationstation if condition of running binary is true (not tested!)
                                    # v1 07/21/17 by cyperghost - Inital run 
                                    # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown
                                    espid=$( pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)" ) # PID of binary only
                                    if [ "$espid" ]; then                                                         # Condition: PID is not equal 0 or empty then use ES shutdown
                                       echo "ES binary is running - I'm using ES internal shutdown"
                                       touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown                     #v1.1 Change file permission of es-shutdown to user:group pi:pi
                                       pkill -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)"
                                       exit
                                    else
                                       echo "ES binary is closed - I proceed with usual shutdown"
                                    fi
                                    # End Emulationstation if condition of running binary is true (not tested!)
                                    
                                    sudo poweroff
                                    fi
                                    done
                                    

                                    Who's Scruffy Looking?

                                    cyperghostC 1 Reply Last reply Reply Quote 1
                                    • cyperghostC
                                      cyperghost @hansolo77
                                      last edited by cyperghost

                                      @hansolo77 Do your testing:
                                      My ratings:

                                      • Shutdown between running gaming session - there is nothing that the script can do. If it works, fine if not then take it easy
                                      • Shutting down in screensavemode - should work
                                      • Shutting down in regular mode - should work (you've tested already)

                                      Have a nice weekend mate - I'm also out for a few hours.

                                      1 Reply Last reply Reply Quote 0
                                      • hansolo77H
                                        hansolo77
                                        last edited by

                                        • Save metadata during game play:
                                          1st Try, Nope. Loaded a game I never played and shutdown during playing. Upon reboot, Last Played had not been updated.
                                          2nd Try, Nope. Added a game I never played to the Last Playedcollection, verified it was there, then started another. I turned off the system while that game was playing. Upon reboot, the collection had not saved.
                                        • Shutting down during screensaver:
                                          1st Try, Nope. I loaded a game, exited, confirmed it existed in the Last Played collection, then let the video screensaver load. When the video was playing, it locked up (video froze, and I was unable recover or access the system via SSH). Had to pull the plug and start over. The list had not saved at this point.
                                          2nd Try, Yup. Same as before, loaded a game, then let the screensaver load. I let it play a few videos to make sure it wasn't locked up, then shut down. Upon rebooting, the Last Played collection had saved.
                                        • Shutting down in regular mode:
                                          1st Try, Yup. via System menu (random system). Loaded a game, verified it was in the Last Played, shut down, and it was still there upon reboot.
                                          2nd Try, Yup. via Favorites menu. Loaded a game, verified Last Played updated, shut down, still there upon reboot.
                                          3rd Try, Yup. via Last Played menu. Loaded a game, verified, shut down, rebooted, still there!

                                        Take away - The new additions to the script are working. Thanks @cyperghost!! The only caveat is that you MUST be in ES when you shutdown if you want it to save your metadata. If you shutdown while playing a game, or if the system crashes, nothing is saved. Perhaps a future update to Retropie can get around this by having the gamelist.xml files be saved upon access (loading a game), rather than after returning from a gaming session.

                                        Who's Scruffy Looking?

                                        pjftP 1 Reply Last reply Reply Quote 0
                                        • pjftP
                                          pjft @hansolo77
                                          last edited by

                                          @hansolo77 The reason that that is not happening is because writing the gamelists is a time consuming process, with time increasing the more content it has. As such, the most effective way to do so is at exit (writing once only).

                                          The metadata is only updated after the game exits and returns to ES, so maybe that's why it's not saving. Try loading a game, exiting from a game, and then loading a game and exiting from within the emulator. See if at least the first game is saved.

                                          1 Reply Last reply Reply Quote 0
                                          • hansolo77H
                                            hansolo77
                                            last edited by

                                            Not sure what you mean. Do you mean load a game from RetroArch via the RetroPie menu?

                                            Who's Scruffy Looking?

                                            pjftP 1 Reply Last reply Reply Quote 0
                                            • 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.