How to Run PulseAudio as a Service via systemd for Root User?

Hi everyone,

I have a Dahlia v1.1B board with the Verdin iMX8M Mini Quad 2GB WB IT V1.1B module. The image I am using is the Toradex Embedded Linux Reference Multimedia Image 7.0.0+build.1 (2024-10-01).

I would like to know how to configure and run PulseAudio as a service using systemd for the root user. Could someone guide me on how to set this up?

Thank you in advance for your support!

Best regards,
Ferran

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Hello Ferran,

Thank you for reaching out with your question. To run PulseAudio as a service using systemd for the root user on your Toradex Verdin iMX8M Mini module, you can follow these steps:

  1. Create a Systemd Service File:

    • Open a terminal on your device.
    • Create a new service file for PulseAudio by running:
      sudo vi /etc/systemd/system/pulseaudio.service
      
    • Add the following content to the file:
      [Unit]
      Description=PulseAudio Sound System
      After=network.target
      
      [Service]
      Type=simple
      ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm --no-cpu-limit
      User=root
      Group=root
      Restart=on-failure
      
      [Install]
      WantedBy=multi-user.target
      
    • Save and exit the editor.
  2. Reload Systemd Configuration:

    • Run the following command to reload the systemd manager configuration:
      sudo systemctl daemon-reload
      
  3. Enable and Start the PulseAudio Service:

    • Enable the service to start at boot:
      sudo systemctl enable pulseaudio.service
      
    • Start the service immediately:
      sudo systemctl start pulseaudio.service
      
  4. Verify the Service Status:

    • Check the status of the PulseAudio service to ensure it is running:
      sudo systemctl status pulseaudio.service
      

This setup will configure PulseAudio to run as a system service under the root user, ensuring it starts automatically on boot and restarts on failure. Adjust the ExecStart line if your PulseAudio binary is located elsewhere or if additional options are needed for your specific use case.

I hope this helps you set up PulseAudio as desired. If you have any more questions, feel free to ask!

Best regards,
Toradex AI Assistant

Please, also check if these links can help you solve your problem:

Integrating Pulseaudio with YOCTO Recipe.

Hi @ferranmc !

From the default Pulseaudio recipe (from OpenEmbedded), seems like it it already equipped with a pulseaudio.service.

Which comes from:

Isn’t it working for you?

And, as we can see here, there is the condition to not be executed as root, since it is not recommended.

Do I guess correctly that you want to execute it as root because of the script shared on Experiencing Stuttering and Audio Interruptions in Bluetooth Calls with Pulseaudio - #23 by bruno.tx (which is in Pulseaudio and module-echo-cancel YOCTO recipe - #15 by bruno.tx)?

Best regards,

Hi @ferranmc !

Do you have any updates on this topic?

Best regards,

Hello @henrique.tx,

Thank you for your response.

What works for me is the script provided by bruno.tx, running it manually when the system boots, but I can’t get it to work when I set it up as a service. When trying to run it as a service, it doesn’t behave as expected.

I would like to know how I can configure PulseAudio to run as a service with a non-root user from Yocto, so I can automate this process without having to do it manually every time. Could you guide me on how to achieve this by creating an appropriate Yocto recipe for this purpose? Also, I need a Yocto recipe that creates a non-root user, runs at boot, and runs PulseAudio as a service.

I really appreciate your help.

Best regards,
Ferran

Hi @ferranmc!

Thank you for the explanation.

So I see 3 different topics in your question:

  1. How to run PulseAudio/the script provided by Bruno as a Systemd service
  2. How to create a recipe in Yocto to have the script added to your custom built image.
  3. How to create a non-root for your custom image using Yocto.

Is my understanding correct?

Would you be able to share exactly what/how have you tried it on your side and what/how it went wrong? No need to touch Yocto right now. I think that it first makes sense to have the Systemd unit working. After that, looking into how to add the service to your custom image should be easier.

Best regards,

Hello @henrique.tx,

That’s correct! The three points you mentioned are the ones I need to address:

  • Run PulseAudio/the script provided by Bruno as a Systemd service.
  • Create a Yocto recipe to add the script to my custom image.
  • Create a non-root user for my custom image using Yocto.

The service was configured as follows:

[Unit]
Description=Pulseaudio Service

[Service]
User=root
Group=root
ExecStart=/home/root/tools/setup_bt2.sh
Type=simple
RemainAfterExit=no
SyslogIdentifier=ES-PULSEAUDIO

[Install]
WantedBy=multi-user.target

However, I can’t get it to work correctly when running it as a service.

Thanks for your help!

Best regards,
Ferran