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:

Description=My Daemon



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

My answer:

Times change and so do best practices.

The current best way to do this is to run systemctl edit myservice, which will create an override file for you or let you edit an existing one.

In normal installations this will create a directory /etc/systemd/system/myservice.service.d, and inside that directory create a file whose name ends in .conf (typically, override.conf), and in this file you can add to or override any part of the unit shipped by the distribution.

For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:


Also note that if the directory exists and is empty, your service will be disabled! If you don’t intend to put something in the directory, ensure that it does not exist.

For reference, the old way was:

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.