Can't start gunicorn with systemd django

18,717

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
Share:
18,717
joel goldstick
Author by

joel goldstick

Presently working for a Travel Consortia maintaining websites for data entry of travel offers, suppliers, member agencies and user accounts. The CMS for member agencies is built with Drupal. The offer entry system (hotels, cruises, etc.) is built with Django. Future enhancements will let these systems talk to Salesforce via the Salesforce api. I'm more interested in Django and python coding lately and I have written offer feeds with python among other internal scripts. I've coded in assemblers, pascal, c, c++, vb, vbscript, a little javascript, php and now python. Moved from Windows to Linux around 2003.

Updated on July 17, 2022

Comments

  • joel goldstick
    joel goldstick almost 2 years

    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?

  • calder-ty
    calder-ty about 7 years
    For more clarification, Even if you have your environmental variables exported in ~/.bash_profile or ~/.bashrc you need to have the environment defined for the service. You can use those files by adding EnviromentFile=home/user/.bash_profile to the [Service] block.
  • Juliano Araújo
    Juliano Araújo over 6 years
    After read any articles, it's works fine... thx for post the solution.
  • NonameSL
    NonameSL over 5 years
    @calder.ty Just a correction: it's EnvironmentFile=/home/user/.bash_profile - notice the n in Environment and the slash prepending the path in order to make it absolute.
  • Maryam Zakani
    Maryam Zakani about 5 years
    You can set environment variables in wsgi file