4

The my-service that is running on the Linux machine needs to be supplied with the ENV_VAR environment variable. It works if I declare this variable in the etc/environment file with:

vim /etc/environment then adding an extra line:

ENV_VAR="My Value"

And finally reloading it with:

source /etc/environment

I then can re-start the my-service with:

sudo service my-service stop sudo service my-service start

I wonder if I could avoid declaring ENV_VAR variable in the etc/environment file. Is there a way to declare the environment variable to be seen by the service using any other approach?

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • 1
    Add `ENV_VAR="My Value"` to `my-service` or to a file which this service includes. – Cyrus Mar 30 '20 at 21:22
  • Please clarify. Or better yet, post it as an answer so we could up-vote it. – alphanumeric Mar 30 '20 at 21:23
  • Your question is better suited to [Unix & Linux Stack Exchange](http://unix.stackexchange.com/tour). – Cyrus Mar 30 '20 at 21:27
  • What distribution are you running? `sudo service my-service stop` suggest you are not using systemd. Which service manager (rc-init, systemd, sysvinit, upstart, etc.) are you using? – KamilCuk Mar 30 '20 at 21:30
  • `Ubuntu DISTRIB_RELEASE=16.04`. It does come with the `systemctl`. Instead of `sudo service my-service start` I can start it with `sudo systemctl start my-service` Is there a way to set up the environment variable using a command line (without messing up with the `my-service.conf` file? – alphanumeric Mar 30 '20 at 21:36
  • So ubuntu uses systemd since some time. So do not use `service`, use `systemctl`. – KamilCuk Mar 30 '20 at 21:36

3 Answers3

3

Is there a way to declare the environment variable to be seen by the service using any other approach?

If your system uses systemd to manage the service, from systemd.exec documentation section "Environment" you can add to your service file:

Environment="ENV_VAR=My Value"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • No, that would be wrong. The environment of the service shouldn't be dependent on the environment from where it was run. Just create a template service (`my_service@argument.service`) if you want to pass arguments to your service. Or I guess as a workaround you could create a drop-in systemd service file with customized configuration, I've seen scripts that do that. – KamilCuk Mar 30 '20 at 21:37
3

So if you want to add an Environment Variable you could do a number of things:

  1. Edit /etc/enviroment (All Users)
  2. Edit /etc/profile (All Users)
  3. Edit ~/.bashrc (Just You)

Editing any of the files above will ensure that any time you startup your machine, your environment will have access to those variables.

You could also edit my-service or a file that my-service includes (as suggested by Cyrus). This means that if you have write access to the source code for the my-service program or a file that my-service includes (calls before it's own execution), you could add a line to that file.

You also don't even have to edit a file! Whenever you launch a terminal you can make live changes to the environment simply by entering the text you would have added to one of the files mentioned above as a command. This environment change will be just like the ~/.bashrc change, in that it will only work for the current user. If you plan to sudo <command>, you will have to first sudo su to switch to the super-user user, and then change the environment.

Weather you are editing a file or entering a command, I suggest using the following line of code. The export command will ensure that environment variable is accessible by my-service.

export ENV_VAR=value

Related question

Lenna
  • 1,220
  • 6
  • 22
  • 1
    Thanks for your answer! But setting up the environment variable in the terminal with `export ENV_VAR=value` and then starting the service with `sudo service my-service start` doesn't make the `ENV_VAR` available to the service. – alphanumeric Mar 30 '20 at 21:43
  • I should have mentioned the user account difference. When you do ```sudo ``` It completes the command as the super-user. You would have to switch to the super user account ```sudo su``` then ```export ENV_VAR=value``` and then ```service my-service start```. I will edit my solution to reflect this – Lenna Mar 30 '20 at 21:48
0

It also works without quotes for example if you want to set environment variables for an asp.net core app;

Environment=ASPNETCORE_ENVIRONMENT=Production ASPNETCORE_URLS=http://localhost:5001

You can set multiple variables using spaces

Never_se
  • 33
  • 1
  • 7