Score:0

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

us flag

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.

Score:0
in flag

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.

Windscale avatar
us flag
Thanks very much. I've gone over that section several times and each time I missed that :-/. I guess I might be able to do /bin/sh -c '...' since that moves the variable expansions into arguments beyond the first.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.