2

In what ways are things different when running an app directly as a user, vs running the same app as a service with systemd?

I wrote an app to play audio cues on my RPI.

It is working perfectly when I run it on my RPI in the terminal (dotnet MyApp.dll) but when systemd starts the same exact app as a service the audio playback is totally distorted.

Any ideas as to what could be the reason?

My service configuration looks like this:

[Unit]
Description=MyApp service
After=network.target

[Service]
ExecStart=/home/pi/.dotnet/dotnet MyApp.dll
WorkingDirectory=/home/pi/app/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
1

1 Answer 1

2

Physical devices like the display and audio (and several others) are assigned to the user locally logged in; and in the case of audio, the audio daemon is run in the user's login session.

As part of the login session, environment variables are set that directs applications to the audio daemon and other services involved in maintaining the session. (There are about 3 relevant environment variables that involve audio, and between one and three of those may be set.)

When you run things as a systemd service, they do not have access to those user and session specific environment variables, and if it is running as a different user than the logged in user, or no user is logged in, then it may not be able to access the devices controlled by the session, even if they have a copy of the environment variables.

This is a problem not just for systemd services but also things like cron jobs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.