9

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?

joel goldstick
  • 4,393
  • 6
  • 30
  • 46

2 Answers2

26

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
joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • 2
    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. – calder-ty Mar 18 '17 at 22:32
  • After read any articles, it's works fine... thx for post the solution. – Juliano Araújo Oct 26 '17 at 18:04
  • 2
    @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. – NonameSL Aug 02 '18 at 20:30
  • You can set environment variables in wsgi file – Maryam Zakani Jan 20 '19 at 13:51
0

Do the following
First you have to create a service file in system

$ sudo nano /etc/systemd/system/python_django.service

Write the following code in service

[Unit]
Description = Python django service
After = network.target

[Service]
ExecStart = /etc/python/python_script.sh

[Install]
WantedBy = multi-user.target

then create a file python_script.sh with the following script in /etc/python/

#!/bin/bash
/usr/bin/gunicorn --access-logfile - -c /etc/python/config/python_django.py  python.wsgi:application

then give the permissions to that file

$ chmod +x python_script.sh

create a file python_django.py in /etc/python/config then put the following code

command = '/usr/bin/gunicorn'
pythonpath = '/home/user/{python_dir}'
bind = '0.0.0.0:8000'
workers = 5
user = 'user'

then run this code

$ sudo systemctl enable python_django //it creates a symlink
$ sudo systemctl start python_django 
$ sudo systemctl status python_django 

Now you can see the running service.

just check http://{ip}:8000 || $ curl "http://0.0.0.0:8000"

Note: please make sure your gunicorn file exists in /usr/bin/gunicorn Check it as $ ls -l /usr/bin/gunicorn

Karthikeyan Ganesan
  • 1,901
  • 20
  • 23