10

I'm creating a service file for a daemon, and I would like to use variables (in init scripts I used environment variables) to define some parameters for the executed scripts. For example, I would like to use 2 parameters $PARAM1 $PARAM2:

[Unit]
Description=my daemon
After=network.target

[Service]
ExecStart=/usr/local/bin/daemon1
PIDFile=/var/run/daemon1.pid
EnvironmentVariable=PARAM1=123
EnvironmentVariable=PARAM2=444
ExecStartPre=-/usr/bin/wget -O - --post-data=key1=$PARAM1&key2=$PARAM2 http://192.168.1.2/log.php
ExecStopPost=-/usr/bin/wget -O - --post-data=key1=$PARAM1 http://192.168.1.2/log.php
Type=simple

[Install]
WantedBy=multi-user.target

Needless to say, this example doesn't work. Is there something like this achievable with systemd? What kind of parametrization of exec commands is possible?

user886869
  • 215
  • 1
  • 2
  • 10
  • The ExecStartPre line uses $PARAM1 and $PARAM2. I'd also dearly like to know if these can be used and where they can be set, which isn't addressed in the current answer. – Andrew Mar 02 '21 at 01:51

1 Answers1

7

The directive is Environment, not EnvironmentVariable.

Environment=

Sets environment variables for executed processes. Takes a space-separated list of variable assignments. This option may be specified more than once, in which case all listed variables will be set. If the same variable is set twice, the later setting will override the earlier setting.

Environment=PARAM1=123
Environment=PARAM2=444

OR

Environment=PARAM1=123 PARAM2=444

Not sure, but you may need to use them inside braces {}:

--post-data=key1=${PARAM1}&key2=${PARAM2} 
Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122