I created init scripts for SysVinit and systemd to distribute with one of our software packages (it should run a non-interactive, background daemon for a network service). The installer asks the user if he wants to install the service; if so, the daemons' user plus home dir are created and the right script (depending on image name of process 1) is copied to the right location.

Since some details depend on the user's specific configuration or company policy (Where to install our tool? What should the daemons user be called? Where should his home directory be?), I used placeholders such as %%TOOL_USER%%, and my setup script replaces those via sed. The automatic stuff works well for CentOS and Debian, so I am already quite happy.

But of course every system is a bit different, so the customer might choose to manually install the service (e.g. if he doesn't use syslog, or just generally has a high level of security, which is the case some of our customers). In that case he needs to adapt the init script.

In the case of sysv init this is easy, since that one is basically a shell script and I just put three variables


on the top, together with some configuration hints.

For systemd on the other hand, I didn't find a possibility to set and reference custom variables, so all I can do is instruct the user to replace all occurrences of %%TOOL_*%% with the appropriate value. While I generally like the way the new systemd scripts work, I would prefer a more failsafe approach like with my init script; now the user might miss an occurrence. Anything I might have missed?

As reference I looked at the freedesktop.org systemd.service docs.


You can use EnvironmentFile=, documented in man systemd.exec, to declare a file where environment variables are stored. The EnvironmentFile can be something your tooling generates based on variables, where the the systemd file can be something manually managed.

Your application will need to be able to respond appropriately when the environment variables are set.

  • 1
    None of the three things that the questioner asked for are subject to variable expansion by systemd in service unit files. See unix.stackexchange.com/questions/199081 and unix.stackexchange.com/questions/242019 . This answer will not actually work. – JdeBP Aug 28 '17 at 19:18
  • I know about EnvironmentFile=, that sets the environment for my daemon and I would use it if it was applicable here. But I would like to have variables set and used exclusively in the init script. As I said, if the user adapts my init script, he should only need to adapt (&understand) a few places and not "everything" (-> JdeBP is right). Seems like it's not possible, though :( – ArchimedesMP Sep 4 '17 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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