0

I'm looking to undo or essentially reset any environment variable changes made by the service when it stops. Can this be done easily by systemd, I haven't found any straight forward way to do so using any of the systemd directives?

For background, I'm working on a product which has been deployed on numerous servers and supports multiple plugins. Certain plugins, in order to start, require custom paths to be set in $PATH and $LD_LIBRARY_PATH environment variables.

Previously this product was started through multiple wrapper scripts that loaded the environment and handled the plugin customisations. However, we have now decided to run it as a service via systemd instead. In order to support the customisations, we have a script which appends the values to the two environment variables dynamically. These are written to a file which is loaded by the EnvironmentFile directive.

This works fine apart from the fact that on every restart the environment variables aren't reset, whilst the dynamic values are appended again. The script can obviously be modified to only append when value not already present, but ideally I'd like to keep it simple and utilise a systemd rather than a script solution.

Portion of the service definition file and the options considered:

[Service]
ExecStartPre=/bin/bash -c '/opt/service/probe_env_configuration.sh > /opt/service/dynamic_env.txt'
EnvironmentFile=-/opt/service/dynamic_env.txt
ExecStart=/opt/latest/probe.linux_64 -port %i -nopassword
#ExecStopPost=unset LD_LIBRARY_PATH                         # <- fails to run unset as it can't find it the path since it's a built-in command
#ExecStopPost=/bin/bash -c 'systemctl unset-environment LD_LIBRARY_PATH'   # <- not suitable, just looking to unset for the service in question
#ExecStopPost=/path/reset_env_vars.sh                       # Would like to avoid another script if possible
#ExecStopPost=Environment=LD_LIBRARY_PATH=                  # Fails with error: Executable "Environment=LD_LIBRARY_PATH=" not found in path

5
  • have you tried to reset the variable in EnvironmentFile directly? calling them two times once as var='' and the second time as var=yourvariable, have no clou if it works, just curious. Commented Apr 29 at 12:17
  • EnvironmentFile is called before ExecStart, but I need it called only when the service stops or restarts. Commented Apr 29 at 12:57
  • A service's environment does not persist from one run to the next. When you start a service, the environment is built from scratch, including only the basics from the system manager, and the Environment=/EnvironmentFile= directives. Therefore ExecStopPost= should not have any effect on the next run. Even /etc/profile.d/ has no effect unless you are running your service in a shell. Commented May 3 at 13:30
  • It would be interesting to understand your use-case a little better. What you're trying to achieve with environments, might be better achieved through other means. Commented May 3 at 13:34
  • In systemd >= 235 UnsetEnvironment= is a thing. See man systemd.exec for details. Commented May 3 at 14:05

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.