Launching a python program with the button

Hello Pisound community, I’m trying to create a button script that will launch a python program. To run the program from the command line I have a short bash script: StartRig.sh that does the job as follows:

#!/bin/sh

/usr/bin/python3 Documents/python3_stuff/rig_FT232H.py

However when I tried to make a button script to launch that same bash script, the python program does not work. Please see my button script below…

#!/bin/sh


. /usr/local/pisound/scripts/common/common.sh

. /home/patch/StartRig.sh

flash_leds 100

This script depends on the current working directory (the location from which it’s executed)

If you change the relative path Documents/python3_stuff/rig_FT232H.py into an absolute path like /home/patch/Documents/python3_stuff/rig_FT232H.py (if using Patchbox OS) or /home/pi/Documents/python3_stuff/rig_FT232H.py (if using Raspberry Pi OS, or just change the ‘pi’ to whatever is your main user), then it can be executed from any context. The pisound-btn scripts are executed from /root/ as the current working directory (often abbreviated as CWD).

Btw, the . at the start of the line is a synonym for source command as described here - it’s for ‘importing’ another script into the current one, so you can call some special functions defined in another script. In your case, you just want to execute your StartRig.sh script

So your StartRig.sh script should look like this:

#!/bin/sh

/usr/bin/python3 /home/patch/Documents/python3_stuff/rig_FT232H.py

# On OSes other than Patchbox OS, change 'patch' above into whatever is the main user on your system

Your button script should look like this:

#!/bin/sh

. /usr/local/pisound/scripts/common/common.sh

/home/patch/StartRig.sh &

flash_leds 100

Final thing to make sure is that chmod +x was done on StartRig.sh and your button script.

Thank you for the explanation, I’ve edited the scripts as per your suggestion. The button is certainly running the program because the led flash operates but the python program does not…yet running the StartRig.sh script from the command line works fine…

You may be able to see some useful output that gets printed by your scripts in the journal:

Run:

journalctl -u pisound-btn -f

It will show the latest messages, so just press the button and see what output you get.

There’s some differences in the run context when scripts are executed from a systemd daemon - some required environment variables might be missing, it runs as different user (root), the current working directory might be different. You may try and add this line:

cd /home/patch

after the . /usr/local/pisound/scripts/common/common.sh line

Yes…this is where it all goes wrong…see below for the journalctl output.

Sep 05 20:41:04 patchbox pisound-btn[6233]: File “/home/patch/Documents/python3_stuff/rig_FT232H.py”, line 8, in
Sep 05 20:41:04 patchbox pisound-btn[6233]: import neopixel_spi as neopixel
Sep 05 20:41:04 patchbox pisound-btn[6233]: ModuleNotFoundError: No module named ‘neopixel_spi’
Sep 05 20:41:04 patchbox sudo[6234]: root : PWD=/home/patch ; USER=root ; COMMAND=/bin/sh -c echo 100 > /sys/kernel/pisound/led
Sep 05 20:41:04 patchbox sudo[6234]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Sep 05 20:41:04 patchbox sudo[6234]: pam_unix(sudo:session): session closed for user root

I got it working… I had to install a python library as root and set an environment variable in the button script then all good.

1 Like

Thanks so much for your help. I’d not understood the implication of the button script running as root. My python install was for user only I suppose.

1 Like

But now a new challenge emerges. My python program runs with one click. I then want to launch Pure Data with a double click but nothing happens… the python program seems to block any further action from the button program until I kill the Python program. I can get round this by launching PD first, but that might sometimes be inconvenient. Can you think of an alternative?

Ah right, you should launch your script as a background job - this is done by adding a ‘&’ character at the end of a script line, so call your script as follows:

/home/patch/StartRig.sh &

YES. Amazing. :grinning:

1 Like