简体   繁体   English

如何设置 Linux 机器上运行的服务要使用的环境变量

[英]How to set the environment variables to be used by the service running on Linux machine

The my-service that is running on the Linux machine needs to be supplied with the ENV_VAR environment variable.在 Linux 机器上运行的my-service需要提供ENV_VAR环境变量。 It works if I declare this variable in the etc/environment file with:如果我在etc/environment文件中声明这个变量,它会起作用:

vim /etc/environment then adding an extra line: vim /etc/environment然后添加额外的一行:

ENV_VAR="My Value"

And finally reloading it with:最后重新加载它:

source /etc/environment

I then can re-start the my-service with:然后我可以重新启动my-service

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.我想知道是否可以避免在etc/environment文件中声明ENV_VAR变量。 Is there a way to declare the environment variable to be seen by the service using any other approach?有没有办法使用任何其他方法声明service可见的环境变量?

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:如果您的系统使用 systemd 来管理服务,您可以从systemd.exec 文档“环境”部分添加到您的服务文件中:

Environment="ENV_VAR=My Value"

So if you want to add an Environment Variable you could do a number of things:所以如果你想添加一个环境变量,你可以做很多事情:

  1. Edit /etc/enviroment (All Users)编辑/etc/enviroment (所有用户)
  2. Edit /etc/profile (All Users)编辑/etc/profile (所有用户)
  3. Edit ~/.bashrc (Just You)编辑~/.bashrc (只有你)

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 ).您还可以编辑my-servicemy-service包含的文件(如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.这意味着,如果您对my-service程序的源代码或my-service包含的文件(在它自己执行之前调用)具有写访问权限,则可以向该文件添加一行。

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.此环境更改将与~/.bashrc更改一样,因为它仅适用于当前用户。 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.如果您打算sudo <command> ,则必须先sudo su切换到超级用户用户,然后再更改环境。

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命令将确保my-service可以访问环境变量。

export ENV_VAR=value

Related question 相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM