Reputation: 4493
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?
Upvotes: 9
Views: 23591
Reputation: 2035
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
Upvotes: 0
Reputation: 4493
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
Upvotes: 26