2016-05-16 129 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發佈解決方案。 –