How to use variables in a systemd service file?

24,162

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} 
Share:
24,162

Related videos on Youtube

user886869
Author by

user886869

Updated on September 18, 2022

Comments

  • user886869
    user886869 over 1 year

    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?

    • Admin
      Admin about 3 years
      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.