How to set environment variable in systemd service?

lfagundes asked:

I have an Arch Linux system with systemd and I’ve created my own service. The configuration service at /etc/systemd/system/myservice.service looks like this:

[Unit]
Description=My Daemon

[Service]
ExecStart=/bin/myforegroundcmd

[Install]
WantedBy=multi-user.target

Now I want to have an environment variable set for the /bin/myforegroundcmd. How do I do that?

My answer:


The recommended way to do this is to create a file /etc/sysconfig/myservice which contains your variables, and then load them with EnvironmentFile.

For complete details, see Fedora’s documentation on how to write a systemd script.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.