2016-05-16 130 views
2

我是systemd的新用户。刚刚安装了lubuntu16.04
我有以下systemd文件:Systemd django无法启动gunicorn

[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 

我得到这个错误:

[email protected]:/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'. 

但是,如果我运行此gunicorn starts

(baseball) [email protected]:~/venvs/baseball/baseball_stats$ /home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi 

我缺少什么,或做错了?

+0

工作目录似乎并不在脚本和shell命令一样。我希望这只是一个错字。 –

+0

好的。我改变了/.venvs到/ venvs但是同样的问题 –

+0

没有journalctl给你更多的东西? – e4c5

回答

9

对于未来的读者,我的问题是由于没有设置django应用程序所需的环境变量而导致的。 (settings.py读取环境)。从命令行调用gunicorn时,该环境变量先前已设置。

当使用systemd以下是在gunicorn.service文件必要:

[Service] 
    Environment=SECRET_KEY=secret 
+0

欲了解更多信息,即使您已将环境变量导出到'〜/ .bash_profile'或'〜/ .bashrc'中,您需要为该服务定义环境。您可以通过将'EnviromentFile = home/user/.bash_profile'添加到'[Service]'块来使用这些文件。 –

+0

阅读任何文章后,它可以正常工作... thx发布解决方案。 –