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 95.6k 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.
    • lostlessL
      lostless @meleu
      last edited by

      @meleu yes

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

        @lostless OK, let's try with that expensive infinite loop just to check if the inotifywait part is bugging the script. Hold on a little...

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

          @meleu I'm trying my own Frankenste in script (taking the parts that i see may work)

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

            @lostless please, try this one:

            #!/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
            
            file="/sys/class/gpio/gpio$GPIOpin1/value"
            
            while true; do
                power="$(cat "$file")"
                if [[ "$power" == 0 ]];
                    sleep 1
                else
                    emu_command="$(sed -n 4p /dev/shm/runcommand.info)"
                    [[ -n "$emu_command" ]] && pkill -f "${emu_command%% *}" && sleep 5
            
                    espid=$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")
                    if [[ "$espid" ]]; then
                        touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown
                        kill "$espid"
                    fi
            
                    sudo poweroff
                fi
            done
            
            • Useful topics
            • joystick-selection tool
            • rpie-art tool
            • achievements I made
            lostlessL 1 Reply Last reply Reply Quote 0
            • lostlessL
              lostless @meleu
              last edited by

              @meleu your new script is still doing nothing and mine worked just like the 1.2.

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

                @lostless helping with this blind coding is very hard. I'm giving up...

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

                  @meleu sounds good man. it was fun. the 1.2 works fine, a lot better then always losing meta data all the time.

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

                    @lostless where is this script placed? what directory?

                    • Useful topics
                    • joystick-selection tool
                    • rpie-art tool
                    • achievements I made
                    lostlessL 2 Replies Last reply Reply Quote 0
                    • lostlessL
                      lostless @meleu
                      last edited by

                      @meleu location is /etc/switch.sh

                      1 Reply Last reply Reply Quote 0
                      • lostlessL
                        lostless @meleu
                        last edited by lostless

                        @meleu My frankenstein script is working. Thank you so much for giving me insight. I know i didnt contribute anything to the code, but I feel like a kid in the 80/90s, discovering computers before they were made easy to use.
                        i found you need to give time for retroarch to quit and emulation station to reload before you try quit emulation station. i gave it 12 seconds. 5 was not enough, by the time the emulator quit, emulation station was not fully loaded back in.

                        here is what i came up with

                        #!/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
                        # v1.0 07/21/17 by cyperghost - Inital run 
                        # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown
                        # v1.2 07/23/17 - Some small improvments, easier to maintain, removed echo, removed else branch 
                        # v1.works 7/27/17 (are we even trying to number this)  @lostless figured it out. More time to quit emulator and reload emulation station before shutdown. @cyperghost  thank you for initial run. @meleu thank you for your insight
                                emu_command="$(sed -n 4p /dev/shm/runcommand.info)"
                                [[ -n "$emu_command" ]] && pkill -f "${emu_command%% *}" && sleep 12
                        
                        espid=$( pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)" ) # detect PID of ES binary only
                        if [ "$espid" ]; then
                           touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown
                           kill $espid
                           exit
                        fi
                        # End Emulationstation if condition of running binary is true
                        
                        sudo poweroff
                        fi
                        done
                        

                        @cyperghost Thank you for the first script up to 1.2.
                        @hansolo77 give this a try. its working for me quite well.

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

                          @lostless can you try this one:

                          #!/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
                                  emu_command="$(sed -n 4p /dev/shm/runcommand.info)"
                                  [[ -n "$emu_command" ]] && pkill -f "${emu_command%% *}" && sleep 12
                          
                                  espid=$( pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)" )
                                  if [ "$espid" ]; then
                                      touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown
                                      kill $espid
                                      exit
                                  fi
                          
                                  sudo poweroff
                              fi
                          done
                          

                          It's exactly the same script as your Frankenstein, but with a nice indentation. If it works I will try one step further...
                          I really would like to avoid that loop every single second!

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

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

                            Testing v1.5 from here now....

                            Take away - Looks like the script is properly closing out of the game I'm playing and returning to ES for a shutdown, but it's not saving the metadata when it shuts that down. I wonder if it's getting hung up in the if functions, not finding a valid true and going to the bottom of the script to the original poweroff without exiting ES first...

                            You are 100% right.... made a small mistake - I forget to add exit command to break loop! Thank you

                            So quick question... anybody know how to make a profile in Putty so it can automatically log in (username/password) so I don't have to keep typing it every time I try one of these tests? :)

                            @lostless - version 1.5 should work now!
                            Sorry... I forget to exit the loop thanks to @hansolo77 he helped me to detect the bug.

                            Please mates try again with v1.5 of exit script (with working exit command now)

                            I give you 100% working guarantee on this ;) You maybe have to extend timer.
                            I would prefer this to @meleu solution because the sleep timer isn't used if there is no emulator running and the PID is detected and stored as $espid and $emupid This script can be extended to play and/or control backgroundmusic for ex. because it extracts PID

                            @meleu Do you think you can break the endless 1 second loop?

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

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

                              @meleu Do you think you can break the endless 1 second loop?

                              yes.

                              I googled about how this device works and I think I found a solution. This is my last atempt...

                              Some notes about what I found:

                              • this script is called at boot (/etc/rc.local)

                              • After changing the script the user needs to reboot to check if it will work.

                              Some notes about the script below, v1.6 (OK, let's put a version to avoid confusion):

                              • I think that at the time of calling the file we are monitoring doesn't exist (that's why I'm putting a sleep before monitoring the file.

                              • I'm not getting why the f&$%k that exit after killing ES is needed. It makes the script stop at the exit, and then the sudo poweroff won't be executed. The strange thing is that the exit is present on v1.2 and @lostless says that it's working...

                              • before this script needs the inotify-tools installed.

                              #!/bin/bash
                              # Before using this script you need to install inotify-tools:
                              #
                              #    sudo apt-get install inotify-tools
                              #
                              # I'm just trying to help with the script logic here.
                              # I DON'T HAVE A MAUSEBERRY AND DIDN'T TEST THE GPIO STUFF HERE!
                              #
                              #########################################################
                              #  U S E   I T   A T   Y O U R   O W N   R I S K ! ! !  #
                              #########################################################
                              #
                              # meleu - July/2017
                              # kudos for @cyperghost , who is very persistent in help you guys! :-)
                              
                              #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
                              
                              file="/sys/class/gpio/gpio$GPIOpin1/value"
                              
                              # up to here this code is executed at booting (/etc/rc.local)
                              # let's wait for the creation of the "/sys/class/gpio/gpio$GPIOpin1/value"
                              sleep 30 # not sure if 30 seconds is a good value...
                              
                              while inotifywait -qq -e modify "$file" ; do
                                  power="$(cat "$file")"
                                  [[ "$power" == 0 ]] && continue
                              
                                  emu_command="$(sed -n 4p /dev/shm/runcommand.info)"
                                  [[ -n "$emu_command" ]] && pkill -f "${emu_command%% *}" && sleep 12 # the value of 12 was suggested by @lostless 
                              
                                  espid=$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")
                                  if [[ "$espid" ]]; then
                                      touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown
                                      kill "$espid"
                              
                                      # I'm not getting why the heck this exit is necessary, it makes
                                      # the "sudo poweroff" not be executed if the logic reaches this point
                                      exit
                                  fi
                              
                                  sudo poweroff
                              done
                              
                              • Useful topics
                              • joystick-selection tool
                              • rpie-art tool
                              • achievements I made
                              cyperghostC 1 Reply Last reply Reply Quote 0
                              • cyperghostC
                                cyperghost @meleu
                                last edited by cyperghost

                                @meleu That's easy....
                                The exit is needed becasue it ends the mausberry GPIO script and gives control to emulationstation.sh for proper exit

                                Can you please exchange your lines

                                    emu_command="$(sed -n 4p /dev/shm/runcommand.info)"
                                    [[ -n "$emu_command" ]] && pkill -f "${emu_command%% *}" && sleep 12 # the value of 12 was suggested by @lostless 
                                

                                with this one?

                                emupid="$(sed -n 4p /dev/shm/runcommand.info)"                                #v1.5 @meleu readout 4th line command.info 
                                emupid="$(pgrep -f "${emupid%% *}")"                                          #v1.5 optain PID emucall by runcommand.sh
                                
                                if [ "$emupid" ]; then                                                        #v1.5 check correct PID > avoid sleeptimer if shutdown from ES
                                    kill $emupid && sleep 10
                                fi    
                                

                                It is really the better way. Because you check value of command.info ... this value will also be active if you are back in ES so your [[ -n "$emu_command" ]] is always true and the sleep timer will used in every situation. Even if an emulator is not active. So this would be the last version called 1.6 release :)

                                This is my last atempt...

                                Haha ... we will see my friend :)

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

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

                                  The exit is needed becasue it ends the mausberry GPIO script and gives control to emulationstation.sh for proper exit

                                  You're right. Good catch!

                                  The nerdy stuff we can discuss on our other thread ;-)

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

                                    @meleu @hansolo77 @lostless

                                    This is also my last posting on this theme!

                                    We have three working versions now!

                                    meleus version 1.6 which got all features of v1.5 but gots faster GPIO response (breaks 1 second loop, use inotify tool - it's not finished now so WIP)

                                    cyperghosts + meleus version 1.56 which will end ES and all emulators called by runcommand.sh

                                    cyperghosts version 1.2 which just ends ES

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

                                      @cyperghost the credit for v1.6 is not meleu's only! :)
                                      By the way, are you sure it really works?

                                      • Useful topics
                                      • joystick-selection tool
                                      • rpie-art tool
                                      • achievements I made
                                      cyperghostC lostlessL 2 Replies Last reply Reply Quote 0
                                      • cyperghostC
                                        cyperghost @meleu
                                        last edited by cyperghost

                                        @meleu Yes it should work. I tested it via SSH only but I also took investigation into the python ESC-script. There is a sleep timer of 10 seconds... so 10 seconds should be a good value :)

                                         for pid in pids:
                                                try:
                                                    commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
                                                    if commandpath[0:24] == '/opt/retropie/emulators/':
                                                        os.system('kill -QUIT %s' % pid)
                                                        print('kill -QUIT %s' % pid)
                                                except IOError:
                                                    continue
                                        

                                        It uses kill PIDcommand to end emulators in path with path /opt/retropie/emulators/

                                        Your RegEx sniplets matches better :) So it works - to 100% I guarantee (tested with N64Glide and zdoom) ... there the python script would fail :)

                                        for v1.6 the GPIO tuning part is 100% yours. So you created it! The others part are nearly same as 1.5 so it should also work.
                                        Finetunig is not included like setting sleeptimers and notify - Do you agree?

                                        You also invested much time and you improved my commands and coding style as I explaind you the need of f%&/! exit command or introduced the second % in pkill -f "${emu_command%% *}" for example. But it is nothing against the && explaination and your RegEx magic, the magic of the testing command, the sed-command, the investigation of the runcommand.info-file ....

                                        So thank you :)

                                        1 Reply Last reply Reply Quote 0
                                        • lostlessL
                                          lostless @meleu
                                          last edited by

                                          @meleu 1.6 does not work. It acts as if the switch is not working. The inotify i don't think is being triggered.

                                          cyperghostC meleuM 2 Replies Last reply Reply Quote 0
                                          • cyperghostC
                                            cyperghost @lostless
                                            last edited by cyperghost

                                            @lostless How about 1.5? Did you check if the script is loaded? Are the permissions are right setted?

                                            lostlessL 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.