i am developing some services scripts that need to be executed on boot on Raspbian (Jessie) and i decided to use systemd. I just started to read some quick tutorials on how to use it, but I have problems with the environment.
The processes that are executed require (in their code) some environment variables that i set in a shell script but they aren't loaded, despite the use of an EnvironmentFile=/path/to/my/file... I have a service executing a Python3 script and another for a Node.js app.
I have been searching for alternative solutions since yesterday, but nothing seems to work... Maybe I just didn't understand how systemd works ? It must be possible to do so, so i am asking you...
Here are my unit files:
For the python script:
[Unit]
Description=My awesome python script
After=multi-user.target
[Service]
ExecStart=/usr/local/bin/python3.6 /home/pi/Desktop/myawesomescript.py
Restart=Always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=mypython
User=pi
EnvironmentFile=/home/pi/.bash_vars
[Install]
WantedBy=multi-user.target
The node app:
[Unit]
Description=My awesome Node.js socket.io app
Requires=After=mypython.service # Requires the python script to be running
[Service]
ExecStart=/usr/bin/node /home/pi/Desktop/myawesomenodeapp/src/index.js
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=mynodeapp
User=pi
EnvironmentFile=/home/pi/.bash_vars
[Install]
WantedBy=multi-user.target
Thank you for taking your time and sorry for my bad english !
PS: Please let me know if you need more informations
Environment can be set in systemd service file as below under Exec options
Environment=LD_LIBRARY_PATH=/usr/lib
Below is the official documentation of systemd Environment/EnvironmentFile usage
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 or the equals sign 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".
See environ(7) for details about environment variables.
EnvironmentFile=
Similar to Environment= but reads the environment variables from a text file. The text file should contain new-line-separated variable assignments. Empty lines, lines without an "=" separator, or lines starting with ; or # will be ignored, which may be used for commenting. A line ending with a backslash will be concatenated with the following one, allowing multiline variable definitions. The parser strips leading and trailing whitespace from the values of assignments, unless you use double quotes (").
The argument passed should be an absolute filename or wildcard expression, optionally prefixed with "-", which indicates that if the file does not exist, it will not be read and no error or warning message is logged. This option may be specified more than once in which case all specified files are read. If the empty string is assigned to this option, the list of file to read is reset, all prior assignments have no effect.
The files listed with this directive will be read shortly before the process is executed (more specifically, after all processes from a previous unit state terminated. This means you can generate these files in one unit state, and read it with this option in the next).
Settings from these files override settings made with Environment=. If the same variable is set twice from these files, the files will be read in the order they are specified and the later setting will override the earlier setting.
Read more here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With