2016-06-16 10 views
7

È possibile fare riferimento ad altre variabili di ambiente quando si impostano nuove variabili in systemd?Riferimento a altre variabili di ambiente in Systemd

[Service] 
EnvironmentFile=/etc/environment 
Environment=HOSTNAME=$COREOS_PRIVATE_IPV4 
Environment=IP=$COREOS_PRIVATE_IPV4 
Environment=FELIX_FELIXHOSTNAME=$COREOS_PRIVATE_IPV4 

Il codice sopra non sembra funzionare.

+1

Sto votando per chiudere questa domanda come off-topic perché è più adatto al sito Unix e Linux. Non è direttamente correlato alla programmazione. http://unix.stackexchange.com/ –

risposta

8

Questa è davvero una domanda per unix & linux. Tuttavia: No, systemd non eseguirà l'espansione delle variabili di ambiente all'interno di Environment=. Da man systemd.exec:

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. If the empty string is assigned to this option, 
     the list of environment variables is reset, all prior assignments have no effect. Variable expansion is not 
     performed inside the strings, however, specifier expansion is possible. The $ character has no special meaning. 
     If you need to assign a value containing spaces to a variable, use double quotes (") for the assignment. 

     Example: 

      Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6" 

     gives three variables "VAR1", "VAR2", "VAR3" with the values "word1 word2", "word3", "$word 5 6". 

Come si vede dall'esempio nella documentazione $word significa solo $word verrà eseguita alcuna espansione. I specificatori che che man parla sono il %i, %n, %u, ecc Sono in man systemd.unit (sotto la propria man sezione).


D'altra parte ExecStart= e suoi derivati ​​eseguirà ambiente espansione variabile. L'utilizzo di variabili d'ambiente su ExecStart= rappresenta la soluzione alternativa comune per le variabili di ambiente extra in systemd. I credo, ovvero uno dei i motivi per cui così tanti programmi recenti accettano gli stessi parametri dall'ambiente e dai parametri della riga di comando.

Un esempio di espansione nel ExecStart=, da man systemd.service:

Example: 

     Environment="ONE=one" 'TWO=two two' 
     ExecStart=/bin/echo $ONE $TWO ${TWO} 

    This will execute /bin/echo with four arguments: "one", "two", "two", and "two two". 

    Example: 

     Environment=ONE='one' "TWO='two two' too" THREE= 
     ExecStart=/bin/echo ${ONE} ${TWO} ${THREE} 
     ExecStart=/bin/echo $ONE $TWO $THREE 

    This results in echo being called twice, the first time with arguments "'one'", "'two two' too", "", and the second 
    time with arguments "one", "two two", "too". 

systemd ha la sua documentazione sparsi tra diverse man s', ma ci si abitua a loro dopo un po'.

Problemi correlati