22

How can I set the Service WorkingDirectory using an environment variable? Here is an example service config:

[Service]
Environment=MYWORKINGDIR=/tmp
WorkingDirectory=${MYWORKINGDIR}

This generates an error along the lines of not an absolute path. Adding a slash to the start "fixes" that error, but the path is still not found:

[Service]
Environment=MYWORKINGDIR=/tmp
WorkingDirectory=/${MYWORKINGDIR}

Is this even possible? Documentation isn't clear on which directives can/can't use env variables.

http://www.freedesktop.org/software/systemd/man/systemd.exec.html

3 Answers 3

21

Is this even possible?

No, It's not possible.

You can use:

  • ~
  • absolute directory path
  • - absolute directory path

Also, WorkingDirectory understands specifiers.

0

One workaround I found was to point WorkingDirectory= to symlink, and change the symlink prior to starting the service.

I know it is not the same; but solved the problem that I was trying to solve with same approach of yours.

0

For all late-comers I have a useful solution here: https://unix.stackexchange.com/a/742644/92366

In short, if you don't need anything too complicated, you could instead use:

ExecStart=/bin/bash -c "cd \"/path/${variable_subpath}/\";echo \"${PWD}\";"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .