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



  • @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.



  • @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?



  • @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



  • @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!



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



  • @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



  • @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.



  • @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!

    :)



  • @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



  • @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.



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.