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. 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. Here is /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. Here is the server.service file:
[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. Here is the syntax I used.
[Service]
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"