runcommand-onstart.sh pass data to python script
-
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
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?
-
@nemecyys not sure about the python part, but the onstart script is right.
The log is placed at
/dev/shm/runcommand.log
. -
Thanks for the quick reply!
Should the runcommand.log be auto generated, or do i have to make it myself before hand?
-
-
@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.
-
@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... -
@nemecyys Well bash works like this
But you will need to perform via sudo as you will need root privilegesgpio_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"
-
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:
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 insideruncommand-onstart.sh
that begins the line with sudo? -
#!/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....
orsudo /home/pi/myfirstscript.sh
asruncommand-onstart
will run just as userpi
-
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""
sosnes
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 @meleuYour both awesome!
- My Python Script was in the wrong location / I didn't define a path so i think it was looking in
-
-
@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
oronend.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.
-
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 upemulationstation
I will give that
ps -a
a try when im inPuTTY
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
oronend.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.