Script works in terminal but not at button press

Hello,

I’m just begining to work with Raspberry Pis and the Pi Sound. As a first challenge to myself, I’ve been trying to set up a script to run from a button press. I have a shell script that calls another script to run some python from a virtual environment. Running the shell script that I’ve mapped to the button works directly from the terminal works fine, but when I press the button the python script doesn’t run.

Here is my script:

#!/bin/sh

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

/home/rm/sstvsetup.sh

flash_leds 100

And here is the error log from the pibutton:

Jan 08 23:45:05 raspberrypi pisound-btn[4478]: script = '/usr/local/pisound/scripts/pisound->
Jan 08 23:45:05 raspberrypi pisound-btn[4478]: script = '/usr/local/pisound/scripts/pisound->
Jan 08 23:45:06 raspberrypi pisound-btn[37162]: qt.qpa.xcb: could not connect to display
Jan 08 23:45:06 raspberrypi pisound-btn[37162]: qt.qpa.plugin: Could not load the Qt platfor>
Jan 08 23:45:06 raspberrypi pisound-btn[37162]: This application failed to start because no >
Jan 08 23:45:06 raspberrypi pisound-btn[37162]: Available platform plugins are: xcb.
Jan 08 23:45:06 raspberrypi pisound-btn[37161]: Aborted
Jan 08 23:45:06 raspberrypi sudo[37170]:     root : PWD=/ ; USER=root ; COMMAND=/usr/bin/sh >
Jan 08 23:45:06 raspberrypi sudo[37170]: pam_unix(sudo:session): session opened for user roo>
Jan 08 23:45:06 raspberrypi sudo[37170]: pam_unix(sudo:session): session closed for user root
lines 136-191/191 (END)

Any advice on what might be going wrong? I’m really eager to figure out what my mistake is!

Hey, looks like the script itself is getting triggered ok. The output of the logs is trimmed, so it’s providing a bit insufficient information. Do this to get it neatly into a file that you could then upload here or copy its contents:

journalctl -u pisound-btn.service > pisound-btn.log

This will create a pisound-btn.log file that you can move from the Raspberry Pi to your computer (ssh & scp are useful for this ) and share the contents here.

What is the /home/rm/sstvsetup.sh script supposed to do?

Hi, thanks for this. I won’t be able to get back up to the Pi Sound until Friday, but I’ll get the log then! The script activates a webcam to take a photo, then it turns the image into an SSTV audio signal and plays it.

Hi again, just got my hands on the log, attaching it below. It looks like, for whatever reason, when the script is activated by the button press it fails to load a plugin necessary to use the open CV library, even though it succeeds when running the same script from the terminal.

pisound-btn.log (24.9 KB)

The systemd services usually run as user root with almost no environment variables set, unless explicitly defined in the .service file or /etc/environment, or exported by the script you run. Also the current working directory might be different.

When it works when launched manually, there’s probably some environment variables already set that make it work.

You can expect what your terminal’s environment looks like just before launching by running this command:

export

You could make your script write the output of export somewhere on your system, near the start of the script, or before last command, add this line:

export > /home/rm/environment.txt

Look for differences in the output, especially look out for any relevant variables to your application you want to run.

Yep, it looks like the environments are very different in the log from the button press and the log for the manual run. I’m attaching them below.

environment_term.txt (3.8 KB)
environment.txt (283 Bytes)

I’ve been working on this problem some more. Here’s my latest error log:

Jan 16 18:44:24 raspberrypi pisound-btn[13648]: Traceback (most recent call last):
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:   File "/home/rm/webcamtests/testa.py", line 53, in <module>
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:     stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:   File "/home/rm/.env/lib/python3.11/site-packages/pyaudio/__init__.py", line 639, in open
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:     stream = PyAudio.Stream(self, *args, **kwargs)
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:   File "/home/rm/.env/lib/python3.11/site-packages/pyaudio/__init__.py", line 441, in __init__
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:     self._stream = pa.open(**arguments)
Jan 16 18:44:24 raspberrypi pisound-btn[13648]:                    ^^^^^^^^^^^^^^^^^^^^
Jan 16 18:44:24 raspberrypi pisound-btn[13648]: OSError: [Errno -9996] Invalid output device (no default output device)
Jan 16 18:44:24 raspberrypi sudo[13665]:     root : PWD=/ ; USER=root ; COMMAND=/usr/bin/sh -c 'echo 100 > /sys/kernel/pisound/led'
Jan 16 18:44:24 raspberrypi sudo[13665]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 16 18:44:24 raspberrypi sudo[13665]: pam_unix(sudo:session): session closed for user root

It seems like when running the script, pyaudio can’t find the output device. The script still works when called from the terminal, and I’ve tried adding the environment variables to my shell script but it still doesn’t seem to work. Feeling pretty stumped!

Looks like PyAudio does not know which output device to use. There’s probably some local config files in /home/rm/… somewhere that tell it what to use, but when it’s running from pisound-btn service context (as root user), it may not be able to find the same config files.

A workaround would be to specify the output device index manually in your script, set output_device_index to some value. Use get_device_count and get_device_info_by_index to know what indices are available.

Thanks for this. This has lead to a kind of interesting discovery. The device index I get from the default user is different than what I get as the root user:

Root user lists the following:

Index: 0, Name: Brio 101: USB Audio (hw:2,0)
Index: 1, Name: pisound: PS-21NNP3F snd-soc-dummy-dai-0 (hw:3,0)

While the regular index is as follows:
Index: 0, Name: Brio 101: USB Audio (hw:2,0)
Index: 1, Name: pisound: PS-21NNP3F snd-soc-dummy-dai-0 (hw:3,0)
Index: 2, Name: pulse
Index: 3, Name: default

For whatever reason, when I try to specify the device, my script only works if I specify 2 or 3, and not the pisound directly. As a result, it doesn’t seem to work at all when I press the button.

You may have to make the pulse audio backend shared between users, try doing these config changes as shown here: [SOLVED] PulseAudio: sharing audio devices between different users / Multimedia and Games / Arch Linux Forums