I need crontab or systemd.timer to run my script

i use a raspberry pi 3b with the Audio DAC SHIM (Line-Out) hooked up. It runs a python script that read input from motion sensors and mixer.music.play() when some1 enters the room.
i set up my audio using the “raspi-config” command

the script runs as expected when launched manually but fails when run as sudo. It also (for the same reason ?) can’t be launched by crontab nor as a systemd.service.

i’ve used systemd.timer and .service with success with sample scripts (also validated the use of “User=user” option)
crontab (and sudo crontab) also works ok when used with sample script

but when i use them to run main script or the periodical check script i always get :

Traceback (most recent call last):
  File "/path/to/script.py", line 41, in <module>
    mixer.init()
pygame.error: ALSA: Couldn't open audio device: Unknown error 524

so far, i used my “/home/user/.bashrc” file to auto-launch my script on start-up, but since it runs continuously it is meant to fail at some point. So i need some kind of daemon to periodically check if it is still running and, if not, relaunch it. Hence the need to use crontab or systemd.

see on pastebin for the code:
• .service file (i tried both “ExecStart” lines) : https://pastebin.com/h17DyKLA
• main script (missing the #! line on pastebin) : https://pastebin.com/aaVFyAQK
• simple periodical check script.sh (which also works fine when run from command line “$ ./script.sh”) :

#!/bin/bash
pgrep python
if [ $? -ne 0 ]; then
        python /path/to/script.py &
        exit
fi

Pygame needs a terminal. When starting from a systemd-service, use something like

[Service]
Type=simple
User=xxx
ExecStart=/usr/bin/nohup /path/to/your/script

At least this worked for me…

Regarding the restarts: systemd can take care of this.

1 Like

thx for the reply, but it didn’t solve the problem. I think nohup acts in very similar way to “RemainAfterExit=true”.

also the service does launch pygame but fails at “mixer.init()”. At least i think so… the following can still be read with ‘systemctl status myservice.service’ :

x naakita.service - check for python scripts, if absent lauch talk2me.py
     Loaded: loaded (/lib/systemd/system/naakita.service; static)
     Active: failed (Result: exit-code) since Tue 2023-12-19 18:41:32 EST; 16s ago
   Duration: 2.312s
    Process: 189135 ExecStart=/usr/bin/nohup /home/gare/naakita/talk2me.py (code=exited, status=1/FAILURE)
   Main PID: 189135 (code=exited, status=1/FAILURE)
        CPU: 2.470s

Dec 19 18:41:30 raspberrypi systemd[1]: Started naakita.service - check for python scripts, if absent lauch talk2me.py.
Dec 19 18:41:32 raspberrypi nohup[189135]: pygame 2.1.2 (SDL 2.26.5, Python 3.11.2)
Dec 19 18:41:32 raspberrypi nohup[189135]: Hello from the pygame community. https://www.pygame.org/contribute.html
Dec 19 18:41:32 raspberrypi nohup[189135]: Traceback (most recent call last):
Dec 19 18:41:32 raspberrypi nohup[189135]:   File "/home/gare/naakita/talk2me.py", line 41, in <module>
Dec 19 18:41:32 raspberrypi nohup[189135]:     mixer.init()
Dec 19 18:41:32 raspberrypi nohup[189135]: pygame.error: ALSA: Couldn't open audio device: Unknown error 524
Dec 19 18:41:32 raspberrypi systemd[1]: naakita.service: Main process exited, code=exited, status=1/FAILURE
Dec 19 18:41:32 raspberrypi systemd[1]: naakita.service: Failed with result 'exit-code'.
Dec 19 18:41:32 raspberrypi systemd[1]: naakita.service: Consumed 2.470s CPU time.

The difference is that nohup will keep a terminal (thus preventing programs from stopping that need a terminal), while RemainAfterExit only controls the behavior within systemd: systemd pretends that the service is still running even if it already finished and exited. So this are two fundamentally different things.

I assume the suggestion from the Pi-forums where you also posted also did not help?!

I found this issue: pygame.error: > No available audio device when running pygame inside systemd with pulseaudio · Issue #412 · pygame/pygame · GitHub

It does not exactly describe your problem, but it is similar. Some other sources suggest that you explicitly try to pass the audio-device to mixer.init(). So two more things to try…

BTW: 524 is ENOTSUP (or ENOTSUPP?!) and hints that the driver is asked to do something that is not supported. Too bad that the message is not more verbose.

1 Like

It did ! I just forgot to subscribe to my post (-‸ლ)

I’ll share the answer in case someone ends up here. The idea is to add the DAC sound card as default device for systemd calls.

cat /proc/asound/cards

and then using the DAC card’s number, i added those 2 lines to ‘/etc/asound.conf’ :

defaults.pcm.card 1
defaults.ctl.card 1
1 Like

Great, thanks for sharing!