How to start a python script at the boot of PatchboxOs?

Hi there,

In fact, I got two questions:

  • First one is: how can I make the choice by default of the soundcard to “Headphones bcm2835_headphones” permanent? It seems to start with vc4HDMI. I changed it via “patchbox” in the jack service settings but i had to change at each reboot…

  • Second one: I got a python script that I want to start automatically at the reboot of the pi.
    I added the following line in crontab, via crontab -e:

@reboot python3 /home/waxmonde/samples/gpio_music_box08.py &

But it doesn’t seem to work. I don’t know if it’s related to the choice of the soundcard at the start but it seems to.

Thanx in advance for your help! :slight_smile:

This is is stored in /etc/jackdrc. Check whether the file contents are correct.

Also check the logs of Jack to see what it is trying to do:

journalctl -u jack

For starting a script automatically, try setting it up as a Patchbox module:

Otherwise, try setting it up as a systemd service, it gives better control like dependency management, so it only attempts to start up once Jack backend is running.

So, I got this in the /etc/jackdrc :

#!/bin/sh
# exec is used to 'morph' the shell interpreter process into jackd process, saving some system resources.
exec /usr/bin/jackd -t 2000 -R -P 95 -d alsa -d hw:Headphones -r 44100 -p 512 -n 3 -X seq -s -S 

It seems to be ok.

Now, the last logs from

journalctl -u jack
-- Boot 456e7fd399274f0ea524f9d047db8718 --
May 17 08:49:24 patchbox systemd[1]: Started jack.service - JACK Server.
May 17 08:49:26 patchbox jackdrc[599]: Failed to create secure directory (/nonexistent/.config/pulse): No such file or directory
May 17 08:49:26 patchbox jackdrc[599]: jackdmp 1.9.21
May 17 08:49:26 patchbox jackdrc[599]: Copyright 2001-2005 Paul Davis and others.
May 17 08:49:26 patchbox jackdrc[599]: Copyright 2004-2016 Grame.
May 17 08:49:26 patchbox jackdrc[599]: Copyright 2016-2022 Filipe Coelho.
May 17 08:49:26 patchbox jackdrc[599]: jackdmp comes with ABSOLUTELY NO WARRANTY
May 17 08:49:26 patchbox jackdrc[599]: This is free software, and you are welcome to redistribute it
May 17 08:49:26 patchbox jackdrc[599]: under certain conditions; see the file COPYING for details
May 17 08:49:26 patchbox jackdrc[599]: JACK server starting in realtime mode with priority 95
May 17 08:49:26 patchbox jackdrc[599]: self-connect-mode is "Don't restrict self connect requests"
May 17 08:49:27 patchbox jackdrc[599]: creating alsa driver ... hw:Headphones|hw:Headphones|512|3|44100|0|0|nomon|swmeter|soft-mode|16bit
May 17 08:49:27 patchbox jackdrc[599]: ALSA: Cannot open PCM device alsa_pcm for capture. Falling back to playback-only mode
May 17 08:49:27 patchbox jackdrc[599]: configuring for 44100Hz, period = 512 frames (11.6 ms), buffer = 3 periods
May 17 08:49:27 patchbox jackdrc[599]: ALSA: final selected sample format for playback: 16bit little-endian
May 17 08:49:27 patchbox jackdrc[599]: ALSA: use 3 periods for playback
May 17 08:49:27 patchbox jackdrc[599]: port created: Midi-Through:midi/playback_1
May 17 08:49:27 patchbox jackdrc[599]: port created: Midi-Through:midi/capture_1
May 17 08:49:38 patchbox jackdrc[599]: port created: RtMidiIn-Client:midi/capture_1
May 17 08:49:38 patchbox jackdrc[599]: port created: RtMidiOut-Client:midi/playback_1
lines 647-681/681 (END)

@Giedrius in fact it’s very strange.
Here’s what I do:

  • everything is via SSH
  • Start the Pi
  • Start the python script → the script starts but when I press a button, nothing happens and it should start the reading of a sound file
  • Quit the script (Ctrl + C)
  • Restart jack via Patchbox command
  • Restart my pyhton script and now it’s ok

So, one of my hypothesis is : as I use pyGame.mixer in my script, could it be possible that the settings I gave in the script are in conflict with the jack settings?

The Jack log seems ok.

What happens if you restart your script without restarting Jack? Does the Jack restart actually succeed? Make sure your script is set up to use Jack, as other options might not be able to share the audio resources. Or if you don’t need Jack at all, you could disable it and use ALSA from your script directly, especially if you don’t need other programs to use the audio at the same time.

The script doesn’t start at the boot, I start it manually (cli). Nothing more happens when I start the script twice wthout restarting Jack.

I’m not sure my script is set up to use Jack. And here it seems to be the problem.

That’s my case! I don’t need others programs to use the audio at the same time. Could I disable jack at the start of PatchboxOs ? And if it is possible, how can I do that?

Thankx for your time and your answers! :hugs:

NB: I stopped jack with jack_control stop , started my script but nothing happened. I stopped the script.
I restarted jack via patchbox.
I restarted the script and then everything was fine.

And I did try this:

waxmonde@patchbox:~ $ jack_control start
--- start
DBus exception: org.jackaudio.Error.Generic: Failed to open server

And then:

waxmonde@patchbox:~ $ cat /proc/asound/cards
 0 [vc4hdmi        ]: vc4-hdmi - vc4-hdmi
                      vc4-hdmi
 1 [Headphones     ]: bcm2835_headpho - bcm2835 Headphones
                      bcm2835 Headphones
waxmonde@patchbox:~ $ jack_control dps device hw:Headphones
--- driver param set "device" -> "hw:Headphones"
DBus exception: org.jackaudio.Error.InvalidArgs: Invalid container address 'driver':'device':'(null)' supplied to method 'GetParameterInfo'.

To disable jack, do this:

sudo systemctl disable --now jack
1 Like

I Did it.
I rebooted the Rasp pi, started the python script and it didn’t start.
I got this message:

pygame.error: Could not connect PulseAudio stream

I don’t know what to do…

NB: I did some research and found this:
https://askubuntu.com/questions/70560/why-am-i-getting-this-connection-to-pulseaudio-failed-error
And this line: pulseaudio --start (start pulseaudio…) did the job.
I restarted the Pi.
And now everything is fine!

Now, I have to configure the Pi to start the python script at the boot!

1 Like

@Giedrius
So I tried to create a systemd service for my script. Here is the result of systemctl status waxmonde.service

waxmonde@patchbox:~ $ systemctl status waxmonde.service 
× waxmonde.service - Lecteur de samples Waxmonde v1.0
     Loaded: loaded (/etc/systemd/system/waxmonde.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2025-05-25 22:32:55 CEST; 44s ago
   Duration: 18.772s
    Process: 605 ExecStart=/usr/bin/python3 /home/waxmonde/samples/gpio_music_box08.py (code=exited, status=1/FAILURE)
   Main PID: 605 (code=exited, status=1/FAILURE)
        CPU: 2.005s

May 25 22:32:54 patchbox python3[605]: pygame 2.1.2 (SDL 2.26.5, Python 3.11.2)
May 25 22:32:54 patchbox python3[605]: Hello from the pygame community. https://www.pygame.org/contribute.html
May 25 22:32:54 patchbox python3[605]: Traceback (most recent call last):
May 25 22:32:54 patchbox python3[605]:   File "/home/waxmonde/samples/gpio_music_box08.py", line 9, in <module>
May 25 22:32:54 patchbox python3[605]:     mixer.init()                                                       #créé une instance de la classe "mixer" de la b>
May 25 22:32:54 patchbox python3[605]:     ^^^^^^^^^^^^
May 25 22:32:54 patchbox python3[605]: pygame.error: ALSA: Couldn't open audio device: Unknown error 524
May 25 22:32:55 patchbox systemd[1]: waxmonde.service: Main process exited, code=exited, status=1/FAILURE
May 25 22:32:55 patchbox systemd[1]: waxmonde.service: Failed with result 'exit-code'.
May 25 22:32:55 patchbox systemd[1]: waxmonde.service: Consumed 2.005s CPU time.

It seems that my script start but Alsa can’t open the audio device (in my case: the headphones)…
So…what can I do???
Here is my waxmonde.service file:

  GNU nano 7.2                                                 /etc/systemd/system/waxmonde.service                                                           
[Unit]
Description=Lecteur de samples Waxmonde v1.0
After=alsa-restore.service
[Service]
ExecStart=/usr/bin/python3 /home/waxmonde/samples/gpio_music_box08.py
WorkingDirectory=/home/waxmonde/samples
User=waxmonde

[Install]
WantedBy=multi-user.target

Thanx for your help…

See similar issues on the Internet, such as this one:

https://forums.raspberrypi.com/viewtopic.php?t=361184

It sounds like the default config is not found, it might be visible from your user (like if it’s in ~/.alsarc), but unavailable in system-wide location.

Alternatively, you could make your systemd unit run as your user, by specifying user, group values (set both to waxmonde) and set HOME environment variable to /home/waxmonde

1 Like

So…Finally it works, thanx to you @Giedrius !!!
Here’s waht I did:

  • went to the linf you specified
  • found this post:

Hi,

I run into similar issue, the problem is that when script is running as service you need to define >default audio device

for me I had to add create file /etc/asound.conf

and add this

pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "hw:2,0"
}
}
  • I rebooted the pi (via ssh, as i don’t need any screen)
  • I pushed one of my button to trigger the play of a sound file and the sound file was played as I can listen it in the headphones!!!

Once again: a BIG thank you Giedrus!!! :star_struck:

1 Like

Good to hear! :clap:

Could you share your final .service file contents? I’m sure it might be useful as a reference to someone else trying to get their audio scripts to start automatically.

Here it is:

[Unit]
Description=Lecteur de samples Waxmonde v1.0
After=alsa-restore.service

[Service]
ExecStart=/usr/bin/python3 /home/waxmonde/samples/gpio_music_box08.py
WorkingDirectory=/home/waxmonde/samples
User=waxmonde

[Install]
WantedBy=multi-user.target

1 Like