Systemd

無法啟動 systemd 服務

  • October 5, 2021

我剛剛用我的 python 燒瓶項目設置並完成了所有工作,我想用它來提供服務。

為了開始,我製作了一個 bash 文件,它應該啟動伺服器,可以位於 /home/pi

#!/bin/bash
cd /home/pi/Desktop/secure-pi-tensorflow
python3 /home/pi/Desktop/secure-pi-tensorflow/run.py

我的服務文件如下所示:

[Unit]
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/securepi.sh

[Install]
WantedBy=multi-user.target

我面臨的問題是我可以使用 bash 文件啟動伺服器,但它沒有作為服務啟動,彈出此錯誤:

pi@securepi:~ $ systemctl status securepi.service
Warning: The unit file, source configuration file or drop-ins of securepi.service changed on disk. Run 'systemctl daemon-reload' to reload units.
● securepi.service
  Loaded: loaded (/lib/systemd/system/securepi.service; enabled; vendor preset: enabled)
  Active: failed (Result: exit-code) since Mon 2019-10-28 15:42:39 GMT; 8min ago
 Process: 1325 ExecStart=/usr/bin/python3 /home/pi/Desktop/secure-pi-tensorflow/run.py (code=exited, status=1/FAILURE)
Main PID: 1325 (code=exited, status=1/FAILURE)

Oct 28 15:42:38 securepi systemd[1]: Started securepi.service.
Oct 28 15:42:39 securepi python3[1325]: Traceback (most recent call last):
Oct 28 15:42:39 securepi python3[1325]:   File "/home/pi/Desktop/secure-pi-tensorflow/run.py", line 12, in <module>
Oct 28 15:42:39 securepi python3[1325]:     from securepi import app
Oct 28 15:42:39 securepi python3[1325]:   File "/home/pi/Desktop/secure-pi-tensorflow/securepi/__init__.py", line 4, in <module>
Oct 28 15:42:39 securepi python3[1325]:     from flask_jsglue import JSGlue
Oct 28 15:42:39 securepi python3[1325]: ModuleNotFoundError: No module named 'flask_jsglue'
Oct 28 15:42:39 securepi systemd[1]: securepi.service: Main process exited, code=exited, status=1/FAILURE
Oct 28 15:42:39 securepi systemd[1]: securepi.service: Failed with result 'exit-code'.

有人知道這個錯誤是怎麼回事嗎?由 bash 文件驅動時,一切執行良好,但在服務嘗試時總是失敗

另外我想提一下,為 python2.7 和 3 安裝的flask_jsglue

pi@securepi:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask_jsglue
[2]+  Stopped                 python3
pi@securepi:~ $ python
Python 2.7.16 (default, Apr  6 2019, 01:42:57) 
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask_jsglue

我所要做的就是添加 User=pi 來解決這個問題。

您只需在

$$ Service $$/etc/systemd/system/my_service.service 文件的部分:

[Unit]
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/securepi.sh
User=your_system_user

[Install]
WantedBy=multi-user.target

引用自:https://unix.stackexchange.com/questions/549145