Can't start gunicorn with systemd django

joel goldstick picture joel goldstick · May 16, 2016 · Viewed 13.5k times · Source

I'm new to systemd. just installed lubuntu16.04.
I have the following systemd file:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=jcg
Group=jcg
WorkingDirectory=/home/jcg/venvs/baseball/baseball_stats
ExecStart=/home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi

[Install]
WantedBy=multi-user.target

I get this error:

jcg@jcg-Inspiron-1011:/var/log$ systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2016-05-16 13:59:18 EDT; 9min ago
  Process: 681 ExecStart=/home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi 
 Main PID: 681 (code=exited, status=200/CHDIR)

May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: Started gunicorn daemon.
May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: gunicorn.service: Main process exited, code=exited, status=200/CH
May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: gunicorn.service: Unit entered failed state.
May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: gunicorn.service: Failed with result 'exit-code'.

But if I run this gunicorn starts:

(baseball) jcg@jcg-Inspiron-1011:~/venvs/baseball/baseball_stats$ /home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi

What am I missing, or doing wrong?

Answer

joel goldstick picture joel goldstick · May 18, 2016

For future readers, my problem was caused by not having an environment variable set that was required by my django application. (settings.py reads the environment). When invoking gunicorn from the command line, that environment variable was previously set.

When using systemd the following is necessary in the gunicorn.service file:

  [Service]
  Environment=SECRET_KEY=secret