Search code examples
linuxservicecentos7systemd

Read variables values from another file into a linux service unit file


I have a normal looking service file in Centos7. My execStart make a call to a shell script for the service code flow.

ExecStart = <Path to my shell script> <var1> <var2>

My requirement is, rather than sending the hardcoded values of var1, var2 etc, I want to read it from a config file and use it in my service file. The config file could look like below:

service_config.txt:
-------------------
var1 = value1
var2 = value2

I must be able to read value1 and value2 from this config inside my service file.


Solution

  • Figured out the solution. Apologies for delayed post.

    In the config file service_config.txt, enter key, values like below:

    var1=value1
    var2=value2
    

    In the service unit file's service section,

    [Service]
    EnvironmentFile=<Absolute_path>/service_config.txt
    

    From here on, the keys are visible using ${var1} ${var2}