无法启动 systemd 服务

无法启动 systemd 服务

我刚刚完成了我的 python Flask 项目的所有工作,我想用它来提供一项服务。

为了开始,我创建了一个 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 文件供电时一切运行良好,但当服务尝试时总是失败

我还想提一下烧瓶_jsglue为 python2.7 和 3 安装

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

答案1

您只需在 /etc/systemd/system/my_service.service 文件的 [Service] 部分添加系统用户(例如 pi 或 ubuntu):

[Unit]
After=multi-user.target

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

[Install]
WantedBy=multi-user.target

答案2

我所要做的就是添加 User=pi 来解决这个问题。

相关内容