You are here
Home > Raspberry >

No Audio With Systemd Command Service on Raspberry Pi

while working on integrating sound playback into a flask service on an RPI. I encountered this issue where No Audio With Systemd Command Service on Raspberry Pi. Th issue where sound would play when running the Python script manually but when running it as a systems systemd service. After troubleshooting, I managed to solve the issue by making adjustments to the system’s service file and sound configuration. here’s a step-by-step guide. The solution was into basics, as you know there is thin line between, process context switch, process state switch and process mode switch. Context switch occurs between two different process while process mode switch occurs between two modes of same process but in these two ‘context’ is saved. Process state is represents process’s life state.

Contents

Problem Summary

  • Environment: Raspberry Pi running a Flask server.
  • Sound Playback Library: Pygame.
  • Issue: Sound played when running the Python script manually with python3 main.py , but did not work when running as a systemd service. No errors were shown, but sound wasn’t heard.

Initial setup

The initial systemd service file looked like this:

[Unit]
Description=Gramton Flask Server
[Install]
WantedBy=multi-user.target
[Service]
User=pi
PermissionsStartOnly=true
WorkingDirectory=/home/pi/backend
Environment="PYTHONUNBUFFERED=1"
ExecStart=python3 main.py
TimeoutSec=600
Restart=on-failure
RuntimeDirectoryMode=755
ExecStartPre=/bin/sleep 2

Step 1: Ensure Proper Audio Access

Running sound services on a Raspberry Pi requires access to audio hardware. I ensured that:

  • The correct user and group permissions were set.
  • The audio group was specified for the service, allowing proper access to the sound subsystem.

Modified Service File

[Unit]
Description=Gramton Flask Server
After=sound.target
[Install]
WantedBy=multi-user.target
[Service]
User=pi
Group=audio # Allows access to audio devices
PermissionsStartOnly=true
WorkingDirectory=/home/pi/backend
Environment="PYTHONUNBUFFERED=1"
ExecStart=python3 main.py
TimeoutSec=600
Restart=on-failure
RuntimeDirectoryMode=755
ExecStartPre=/bin/sleep 2

Step 2: Adjust Environment Variables

In addition to granting audio access, I added environment variables to ensure compatibility with the Raspberry Pi’s graphical environment:

  1. DISPLAY: Setting the DISPLAY=:0 environment variable ensures the sound can play in the context of the desktop environment (X11).
  2. XDG_RUNTIME_DIR: This variable was added to ensure access to PulseAudio, which is responsible for managing audio on the system.

Environment="DISPLAY=:0" # Ensures sound can play on the active display
Environment="XDG_RUNTIME_DIR=/run/user/1000" # Ensure PulseAudio access

Step 3: Check and Increase Volume Using ALSA Mixer

Sometimes the system volume is too low to hear anything, even when the sound plays correctly. To fix this:

  1. Open ALSA Mixer: Run alsamixer in the terminal.
  2. Increase Volume: Use the arrow keys to navigate and adjust the volume. Ensure that the appropriate sound outputs (e.g., PCM or Master) are turned up.
  3. Unmute: If any sound outputs are muted, press M to unmute them.

This step is essential to make sure the Raspberry Pi’s output volume is audible.

Final Systemd Service File

Here’s what the final service file looked like after all the adjustments:

[Unit]
Description=Gramton Flask Server
After=sound.target
[Install]
WantedBy=multi-user.target
[Service]
User=pi
Group=audio
PermissionsStartOnly=true
WorkingDirectory=/home/pi/backend
Environment="PYTHONUNBUFFERED=1"
Environment="DISPLAY=:0" # Set display for GUI apps (PulseAudio, etc.)
Environment="XDG_RUNTIME_DIR=/run/user/1000" # Ensure proper PulseAudio access
ExecStart=python3 main.py
TimeoutSec=600
Restart=on-failure
RuntimeDirectoryMode=755
ExecStartPre=/bin/sleep 2

Conclusion

After making these changes, the sound played correctly both when running the script manually and as a systemd service. The key elements were ensuring that the service had proper permissions to access audio hardware and setting up the correct environment variables for sound playback.

By following these steps, you should be able to successfully resolve sound issues in Flask services running on a Raspberry Pi. Hope you have enjoyed reading No Audio With Systemd Command Service on Raspberry Pi, happy debugging.

Top