Environment variables in systemd ExecStart, ExecStop clauses not working as expected

environment-variableslinuxservicesystemd

I'm working on some unit files for some systemd services. The path prefix to some of my binaries is quite long and they are used several times in ExecStart=, ExecStartPost=, and ExecStop= clauses.

I have some environment variables defined (which also need to be passed to the service) e.g:

Environment=ORACLE_HOME=/u01/app/oracle/product/19/db_1

I believe, from the "Command lines" section in systemd.service(5) that I should be able to use variable expansion in the Exec*= clauses – e.g:

ExecStart=${ORACLE_HOME}/bin/dbstart

When I try that and do a systemctl daemon-reload I get the following error in the journal:

systemd[1]: [/usr/lib/systemd/system/oracle.service:37] Executable path is not absolute, ignoring: ${ORACLE_HOME}/bin/dbstart /u01/app/oracle/product/19/db_1

The systemd docs are fairly clear that the path to the file to run must start (except for the allowed prefixes) with / but is there some nuance of the syntax I'm missing that would enable me to do what I want?

Eventually, I'd like to have files containing the environment variables that are used so they can be used in the systemd unit files and also for setting up profiles etc.

I'm using RHEL7 and RHEL8.

Best Answer

You can use environment variables, but not as the command name. The "Command lines" section of systemd.service(5) states that

Note that the first argument (i.e. the program to execute) may not be a variable.