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

    runcommand-onstart.sh pass data to python script

    Scheduled Pinned Locked Moved Help and Support
    runcommandonstartpythonshellbash
    17 Posts 3 Posters 3.1k 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.
    • N
      Nemecyys
      last edited by Nemecyys

      Hey,

      I wanted to pass the information of the system currently in play in RetroPie from the runcommand-onstart.sh script to a python file, so i can toggle on some GPIO pins.

      the python code is located in the same folder as the runcommand-onstart.sh
      0_1507833455239_RPI.JPG

      I can't seem to find the runcommand.log file either - so i'm not sure if its even running correctly to begin with.

      Here is my code:

      #!/usr/bin/env bash
      
      # Get system name
      system=$1
      
      
      # Write to Runcommand Log
      echo "I succesfully ran ON START" >&2
      echo "$system" >&2
      
      # Call my Python code and pass variable
      sudo python Arduino_Emu.py "$system"
      

      Python

      import RPi.GPIO as IO
      import time
      import sys
      
      system = sys.argv[1]
      
      IO.setmode(IO.BCM)
      pin1 = 22
      pin2 = 23
      pin3 = 24
      
      IO.setup(pin1, IO.OUT)
      IO.setup(pin2, IO.OUT)
      IO.setup(pin3, IO.OUT)
      
      if system == snes:
          IO.output(pin1, IO.LOW)
          IO.output(pin2, IO.LOW)
          IO.output(pin3, IO.HIGH)
          print("SNES")
      
      else:
          print("Found nothing")
      

      Where am i going wrong?

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

        @nemecyys not sure about the python part, but the onstart script is right.

        The log is placed at /dev/shm/runcommand.log.

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

          @meleu

          Thanks for the quick reply!

          Should the runcommand.log be auto generated, or do i have to make it myself before hand?

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

            @Nemecyys Well you redirect to this file. So it's generated by you and it's always generated new by runcommand (@meleu was a bit faster). But it will be deleted after every reboot/restart as dev/shm is a RAMdrive

            But why not use bash instead of python?

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

              @nemecyys said in runcommand-onstart.sh pass data to python script:

              Should the runcommand.log be auto generated, or do i have to make it myself before hand?

              Auto-generated and overwritten every time the runcommand script is executed.

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

                @cyperghost said in runcommand-onstart.sh pass data to python script:

                @Nemecyys Well you redirect to this file. So it's generated by you and it's always generated by runcommand.

                But why not use bash instead of python?

                Sorry i meant to also add, the file isn't there, I just went to look.

                I am looking via Raspbian though (I installed RetroPie on top) , so I assume it should still exist from a previous time it was ran?

                It's deleted after every "Reboot" of the RPI3 is it?

                Im not good at bash @cyperghost, so I thought it would be easier to just run the python script i knew worked.

                @meleu said in runcommand-onstart.sh pass data to python script:

                @nemecyys said in runcommand-onstart.sh pass data to python script:

                Should the runcommand.log be auto generated, or do i have to make it myself before hand?

                Auto-generated and overwritten every time the runcommand script is executed.

                Hmm, odd that i can't see it :S
                Unless like cypher says, its deleted the moment i reboot...

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

                  @nemecyys Well bash works like this
                  But you will need to perform via sudo as you will need root privileges

                  gpio_lcd=21
                  # Init
                  echo $gpio_lcd > /sys/class/gpio/export
                  
                  # Set direction (in/out)
                  echo out > /sys/class/gpio/gpio$gpio_lcd/direction
                  
                  # Set value 1=on, 0=off
                  echo 1 > /sys/class/gpio/gpio$gpio_lcd/value
                  echo 0 > /sys/class/gpio/gpio$gpio_lcd/value
                  
                  # Reset
                  echo $gpio_lcd > /sys/class/gpio/unexport
                  

                  String compare with test-command

                  [[ "$1" == "snes" ]] && echo "SNES"
                  
                  1 Reply Last reply Reply Quote 1
                  • cyperghostC
                    cyperghost @Nemecyys
                    last edited by cyperghost

                    @Nemecyys

                    Hmm, odd that i can't see it :S
                    Unless like cypher says, its deleted the moment i reboot...

                    Well then redirect the echo runcommand-onstart.sh to a file in your home directory. Just for test how far your call progress is.

                    N 1 Reply Last reply Reply Quote 1
                    • N
                      Nemecyys @cyperghost
                      last edited by Nemecyys

                      @cyperghost said in runcommand-onstart.sh pass data to python script:

                      @Nemecyys

                      Hmm, odd that i can't see it :S
                      Unless like cypher says, its deleted the moment i reboot...

                      Well then redirect the echo runcommand-onstart.sh to a file from your home directory. Just for test how far your call progress is.

                      @cyperghost how do i redirect the echo?

                      and Thanks for the example bash script, I will give that a go too!

                      Im new to all three of the languages i'm using right now: Shell, Python and C++ (Arduino)
                      So im struggling to get all the syntax correct.

                      Thanks for the advice :)

                      EDIT:
                      You mentioned running the bash via "sudo"

                      Will runcommand-onstart.sh run with "sudo" or should i run another bash from inside runcommand-onstart.sh that begins the line with sudo?

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

                        @nemecyys

                        #!/usr/bin/env bash
                        
                        # Get system name
                        system=$1
                        
                        
                        # Write to Runcommand Log
                        echo "I succesfully ran ON START" >> $HOME/test.log
                        echo "$system" >> $HOME/test.log
                        
                        # Call my Python code and pass variable
                        sudo python Arduino_Emu.py "$system"
                        

                        Then your echo strings are stored to your homedir in file test.log

                        Use the same sudo command as in your script .... sudo python.... or sudo /home/pi/myfirstscript.shas runcommand-onstartwill run just as user pi

                        N 1 Reply Last reply Reply Quote 2
                        • N
                          Nemecyys @cyperghost
                          last edited by Nemecyys

                          @cyperghost

                          You Living Legend!

                          I managed to get it to work, there were a few problems.

                          • My Python Script was in the wrong location / I didn't define a path so i think it was looking in $HOME
                          • My Python Script also required the IF statment to contain "" so snes needed to be "snes"

                          So thank you so much for your quick replies and patience, I really appreciate it!!

                          I've learnt a lot from this and once i get this up and running I will definitely look at recreating it in bash so I can learn more about this language as well!

                          Thank you so much @cyperghost
                          and you too @meleu

                          Your both awesome!

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

                            @nemecyys well thx but @meleu is the one of great codemasters here as he showed me things to get tidy on bash coding ;)

                            N 1 Reply Last reply Reply Quote 0
                            • N
                              Nemecyys @cyperghost
                              last edited by

                              @cyperghost said in runcommand-onstart.sh pass data to python script:

                              @nemecyys well thx but @meleu is the codemaster here as he showed me things to get tidy on bash coding ;)

                              Aha well perhaps I can learn from you both then ;)

                              Oh, on a technical note.

                              Because im running this python script every time i run onstart.sh or onend.sh does that mean there will be LOTS of instances of it running?

                              Just incase im going to add an exit() command to the end of every if statement, as that is essentially all it needs to do.

                              With a Bash Script:
                              Would this also be the case?
                              Would i want to exit the bash after running and achieving the desired result?

                              also, any links to some of meleu's coding goodness?
                              i'd like to get my bash coding on point too
                              - ;)

                              EDIT:
                              Upon testing that in python, it does seem to leave my GPIO pins in their set state after quitting, so this is a clean way to run the python script and close it

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

                                @nemecyys
                                Well sure... contribute here
                                You are welcome...

                                I'm not very common in python. But as it is a modern language I think there should be no exit-command written. I think an exclusiv exit is better to give proper return codes. But you can check with ps -a command in SSH shell if there is a python instance still running.

                                Via bash - the last command line automatically ends the call.

                                Would i want to exit the bash after running and achieving the desired result?

                                Sorry didn't get you. If you wait for an input via LOOP then the bash script will run endless until break event or condition clause. But if you just want to set some GPIOs low/high and print out an echo command the bash-script is proper closed after last command.

                                N 1 Reply Last reply Reply Quote 0
                                • N
                                  Nemecyys @cyperghost
                                  last edited by

                                  @cyperghost

                                  ahh ok - I don't have a keyboard spare for the RPI right now, so im using a mix of VNC to control the Desktop and PuTTY to control the CMDline which lets me boot up emulationstation

                                  I will give that ps -a a try when im in PuTTY

                                  By "exclusive exit" you mean write my own?
                                  So i can pass variables and data across?

                                  What i was sort of getting at was "If i exit the bash, will it still keep the GPIO pins in the state i left them"

                                  Ahh right if the bash ends once it reaches the last line then i should be ok, as long as i write in proper echos to explain my issue the test.log should keep track of that so i can review it after.

                                  Also thanks for the link this will make for some great night time reading!

                                  :)

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

                                    @nemecyys said in runcommand-onstart.sh pass data to python script:

                                    "If i exit the bash, will it still keep the GPIO pins in the state i left them"

                                    Yes of course! The unix system treats every device as file system. So if you write a 1 to the "file" GPIOPIN14 it stays a 1 - still you write annother value in. Or "delete" the file by using the echo unexport command.

                                    Yes via putty you can enter CLI and give commands ...

                                    Here is also an interesing thread about bash scripting and how to get some improvements

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

                                      @nemecyys said in runcommand-onstart.sh pass data to python script:

                                      Because im running this python script every time i run onstart.sh or onend.sh does that mean there will be LOTS of instances of it running?

                                      On normal typical usage, no. The runcommand script calls the onstart one and waits it finish. The way you're invoking your python script in onstart also makes it wait the python script to finish.

                                      Then, from what I'm seeing from the codes, I believe that there won't be lots of instances running.

                                      With a Bash Script:
                                      Would this also be the case?
                                      Would i want to exit the bash after running and achieving the desired result?

                                      There's no need.

                                      *also, any links to some of meleu's coding goodness?

                                      You can find links to some of my scripts on the "useful topics" post (link on my signature). You can find many other useful info there. ;-)

                                      Oh, as @cyperghost said, we usually use this thread to talk about shell scripting.

                                      By the way, you asked above and I'm not sure if you got an answer: the /dev/shm/runcommand.log file is deleted after a shutdown/reboot.

                                      • Useful topics
                                      • joystick-selection tool
                                      • rpie-art tool
                                      • achievements I made
                                      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.