システムサービスを開始できません

システムサービスを開始できません

私はPython Flaskプロジェクトのすべての作業を終えて、それを使ってサービスを提供したいと思います。

始めるためにサーバーを起動し、/home/piに配置できるbashファイルを作成しました。

#!/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ファイルを使用するとすべてがうまく動作しますが、サービスを試しても常に失敗します。

私も言いたいフラスコ_jsgluePython2.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

おすすめ記事