How to specify an Environment systemd directive containing =?

71,233

I think your problem is due the space in the environment variable's contents. Looking at the examples from the systemd docs, an assignment should be a single string:

Example:

Environment="ONE=one" 'TWO=two two'
ExecStart=/bin/echo $ONE $TWO ${TWO}

This will execute /bin/echo with four arguments: one, two, two, and two two.

Example:

Environment=ONE='one' "TWO='two two' too" THREE=
ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
ExecStart=/bin/echo $ONE $TWO $THREE

This results in echo being called twice, the first time with arguments 'one', 'two two' too, , and the second time with arguments one, two two, too.

I tested this with the following service (note the quotes around the entire assignment):

[Unit]
Description=My Daemon

[Service]
Environment='CATALINA_OPTS=-Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current'
ExecStart=/bin/echo ${CATALINA_OPTS}

[Install]
WantedBy=multi-user.target

And got the desired output in journalctl:

Apr 26 08:19:29 laptop echo[28439]: -Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current

Of course, it would be simpler to use EnvironmentFile instead. Replacing the Environment with the following gave the same desired result:

EnvironmentFile=/tmp/foo

Where /tmp/foo contained (note the lack of quotes):

CATALINA_OPTS=-Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current
Share:
71,233

Related videos on Youtube

Kalle Richter
Author by

Kalle Richter

Updated on September 18, 2022

Comments

  • Kalle Richter
    Kalle Richter over 1 year

    I want to specify an Environment systemd directive containing =, e.g.

    Environment=CATALINA_OPTS=-Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current
    

    and get the error

    [/lib/systemd/system/archiva.service:10] Invalid environment assignment, ignoring: CATALINA_OPTS=-Dappserver.home\=/var/lib/archiva/apache
    

    in journalctl -xe. I tried to quote with " and ' and to escape = with \ without success. This seems undocumented.

  • Kalle Richter
    Kalle Richter about 9 years
    When it comes to quoting quotes (e.g. by using CATALINA_OPTS in systemd environment for Apache tomcat 7.0.61) using EnvironmentFile really is the way to go. Thanks!
  • Davos
    Davos over 6 years
    What is the standard/ convention directory for keeping an EnvironmentFile on Ubuntu? On other systems I've seen /etc/sysconfig/
  • muru
    muru over 6 years
    @Davos a reasonable place would be /etc/default. Files there have been historically used for placing environment variables for corresponding init scripts.
  • Davos
    Davos over 6 years
    I have this file already on my system /etc/environment which contains a PATH variable, would appending to that be sensible?
  • muru
    muru over 6 years
    @Davos that's systemwide. If there's no problem with the variable being set for practically every process, sure. Note that /etc/environment is not processed by a shell; aside from simple variable assignment, its syntax is very different from the systemd syntax noted above or general shell syntax.
  • Davos
    Davos over 6 years
    Thanks for the guidance, that's very helpful. I'll use something like /etc/default/<servicename> as the EnvironmentFile for this.
  • whirlwin
    whirlwin over 4 years
    systemd is so nice.....
  • lav
    lav about 4 years
    Is it possible to set a variable with a double quote in the value?
  • muru
    muru about 4 years
    @lav quoting the value with single quotes should work, I guess, but I haven't tested it