Pythonを実行するサービスの作成

Pythonを実行するサービスの作成

.serviceファイルを生成してPi4でサービスを開始しようとしています。

これが私が今まで持っているものです:

Unit]
Description=NZBHydra2 Daemon
Documentation=https://github.com/theotherp/nzbhydra2
After=network.target

[Service]
User=root
Group=something
Type=simple

# Set to the folder where you extracted the ZIP
WorkingDirectory=/opt/nzbhydra2

# NZBHydra stores its data in a "data" subfolder of its installation path
# To change that set the --datafolder parameter:
# --datafolder /path-to/datafolder
ExecStart=sudo -u pi /usr/bin/python /opt/nzbhydra2/nzbhydra2wrapper.py --nobrowser
Restart=on-failure

[Install]
WantedBy=multi-user.target

追加したpi代わりにユーザーとして実行したいのですが、Pythonアクセスは拒否されました。なしで実行すると、ルートのみアクセスできるファイルも作成されます。 /opt/nzbHydra2の所有者はpi:piです。root-u pi-u

pirootではなく、ユーザーとしてこのPythonスクリプトを実行しながらもPythonにアクセスできる方法は何ですか?

ありがとうございます!

ベストアンサー1

sudoシステムサービスや非対話型スクリプト内では使用しないでください。代わりに、すでに検索されているUser=フィールドを使用できます。Group=

お使いのデバイスは次のようになります。

[Unit]
Description=NZBHydra2 Daemon
Documentation=https://github.com/theotherp/nzbhydra2
After=network.target

[Service]
User=pi
Group=pi
Type=simple
WorkingDirectory=/opt/nzbhydra2
ExecStart=/usr/bin/python /opt/nzbhydra2/nzbhydra2wrapper.py --nobrowser
Restart=on-failure

[Install]
WantedBy=multi-user.target

おすすめ記事