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
Environment=
/EnvironmentFile=
directives. ThereforeExecStopPost=
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.UnsetEnvironment=
is a thing. Seeman systemd.exec
for details.