How do I set an env var for systemd ExecStart command in Linux?

I've installed Snapclient on a raspi (a program which receives streaming music and plays it). It has a systemd service file which among others contains this line:

ExecStart=/usr/bin/snapclient --logsink=system $SNAPCLIENT_OPTS

I now want to set the $SNAPCLIENT_OPTS so I added the following line to /etc/environment:

SNAPCLIENT_OPTS="--host 192.168.178.79"

After a reboot and an ssh login as root I see that the env is set:

root@dietpi:~# env | grep SNAPCLIENT
SNAPCLIENT_OPTS=--host 192.168.178.79

However, the service still doesn't run with that argument:

root@dietpi:~# ps aux | grep snap
_snapcl+ 525 0.4 1.4 16632 7000 ? Ss 15:36 0:00 /usr/bin/snapclient --logsink=system

In the line above I do see that it isn't run as root though, but as the user _snapclient, which explains that the system wide env var isn't picked up. I guess I could set an env var for the user _snapclient, but that user doesn't even have a home directory. I can also change the systemd service file, but that seems like a hack.

What would be the correct way to set this env var and have it used when the snapclient service is started?

1 Answer

If snapclient.service works this way, it should also have an EnvironmentFile= setting that reads from /etc/default, /etc/conf.d, or some similar location. Put your environment variables there.

In case there's no EnvironmentFile specified, use systemctl edit to directly add Environment= directives to the service:

[Service]
Environment=SNAPCLIENT_OPTS="--host 192.168.178.79"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like