2017-09-22 143 views
0

我使用的是Ubuntu 16,我想啓動該服務。服務應該在系統啓動時自動啓動。該服務啓動django服務器。如何在Ubuntu啓動時自動啓動服務?

[Unit] 
Description=service 

[Install] 
WantedBy=multi-user.target 

[Service] 
ExecStart=/usr/bin/python /home/ubuntu/wiki/Backend/manage.py python runserver 0.0.0.0:8000 
Type=simple 

在控制檯錯誤:

● wiki.service - service 

Loaded: loaded (/etc/systemd/system/wiki.service; enabled; vendor preset: enabled) 

Active: failed (Result: exit-code) since Fri 2017-09-22 11:10:44 UTC; 3min 36s ago 

Main PID: 1144 (code=exited, status=1/FAILURE) 

systemd[1]:Started service. 

python[1144]:Traceback (most recent call last): 

python[1144]:File "/home/ubuntu/wiki/Backend/manage.py", line 17, in <module> 

python[1144]: ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment? 

systemd[1]: wiki.service: Main process exited, code=exited, status=1/FAILURE 

systemd[1]: wiki.service: Unit entered failed state. 

systemd[1]: wiki.service: Failed with result 'exit-code'. 
+0

你有任何virtualenv? – itzMEonTV

+0

@itzMEonTV,是 – user233428

+0

因此,不要使用'/ usr/bin/python',嘗試'/ path/to/env/bin/python' – itzMEonTV

回答

0

默認情況下,所有的systemd服務以root身份運行。因此,它在啓動服務期間將root用戶的環境用於所有內容。這可能是這裏的問題。這可能是django的東西需要像你一樣運行。要檢查這一點,你可以讓systemd以特定用戶身份啓動服務。進行以下更改服務文件:

[service] 
User=<whatever your username is> 
ExecStart=/usr/bin/python /home/ubuntu/wiki/Backend/manage.py python runserver 0.0.0.0:8000 
Type=simple 

看看是否能解決問題

服務節還可以有一個組的參數。包括這一點可能不會受到傷害。