简体   繁体   English

设置系统环境变量

[英]Set a system environment variable

I have a flask server running and I can not get it to run as a service without it dumping all the environment variables.我有一个正在运行的烧瓶服务器,如果不转储所有环境变量,我就无法让它作为服务运行。

Here is my config that is supposed to use sqlite if there is no DATABASE_URL environment variable.如果没有 DATABASE_URL 环境变量,这是我应该使用 sqlite 的配置。 It works everytime unless I run it as a service at which point it never works.它每次都有效,除非我将它作为服务运行,而此时它永远不会工作。

#SQL Alchemy Database config
if os.environ.get('DATABASE_URL') is None:
    print('Using SQLITE')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_DATABASE_URI = ('sqlite:///' + os.path.join(basedir, 'app.db') + '?check_same_thread=False')
else:
    print('Using MySQL')
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    #SQLALCHEMY_DATABASE_URI = ('mysql+pymysql://username:password@localhost/20200830MYSQL')
    #export DATABASE_URL="mysql+pymysql://username:password@localhost/20200830MYSQL"

Placing it in /etc/environment works for when I manually run, but not the service.当我手动运行时,将它放在 /etc/environment 中有效,但不适用于服务。 Here is /etc/environment这是/etc/environment

DATABASE_URL="mysql+pymysql://username:password@localhost/20200830MYSQL"

Doing the following command in a terminal does not work for my service as well.在终端中执行以下命令也不适用于我的服务。

export DATABASE_URL="mysql+pymysql://username:password@localhost/20200830MYSQL"

I've also placed it in the server.service file in /etc/systemd/systemand it does not work still.我还把它放在 /etc/systemd/system 的 server.service 文件中,但它仍然无法工作。 Here is the server.service file:这是 server.service 文件:

[Unit]
Description=Server
After=multi-user.target

[Service]
DATABASE_URL="mysql+pymysql://username:password@localhost/20200830MYSQL"
WorkingDirectory=/home/jon/server
ExecStart=/home/jon/server/env/bin/gunicorn -k gevent -b 0.0.0.0:5080 app:app
Restart=always

[Install]
RequiredBy = multi-user.target

As detailed here I needed to correct some syntax when I placed the variable into the server.service.如此处详述当我将变量放入 server.service 时,我需要更正一些语法。 Here is the syntax I used.这是我使用的语法。

[Service]
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"

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

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