4

I have a nodeJS service built using NodeJs. This service requires some environment variables to be passed to it. Moreover, I created a systemd unit file to start it from systemctl. For some weird reasons, the service, when started with systemctl, does not read the environment variables. For instance, one environment variable is the HOST, which contains the IP to which the sails app will be binded. However, if I start the service with sails lift or node app.js, it does read the environment variables. Here is the unit file:

[Unit]
Description=project

[Service]
ExecStart=/usr/bin/node /mnt/project/app.js
Restart=always
StandardOutput=syslog

[Install]
WantedBy=multi-user.target

I tried everything. I added the environment variables to /etc/environment and pointed the unit file to it, I also added them to the unit file, but nothing worked.

6

This happens because a unit in systemd is encapsulated from rest of the system. Have you tried setting it directly in the unit file like this?

Environment="FOO=foo" 'BAR=bar'

The variables FOO and BAR are then available in your unit. For example if this is my ExecuteStart:

ExecStart=/bin/echo $FOO ${FOO} ${BAR}

With the Environment option from above this would result in the following output:

foo foo bar

An alternative would be doing it via a environment config file. For this you can read the fedora dokumentation:

http://fedoraproject.org/wiki/Packaging:Systemd#EnvironmentFiles_and_support_for_.2Fetc.2Fsysconfig_files

This link contains some useful examples with docker:

https://coreos.com/os/docs/latest/using-environment-variables-in-systemd-units.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.