開始する正しいサービス「タイプ」

開始する正しいサービス「タイプ」

Pythonスクリプトを介して実行されるLCDパネルがあり、パネルがキーストロークを受け取るので、スクリプトは常に受信されます。コンピュータが起動したらすぐにスクリプトを実行するスタートアップサービスを作成しようとしています。

私が読んだところによると、Type=simpleこれは最もうまくいきますが、スクリプトは実行されません。しかし作品Type=oneshotはありますRemainAfterExit=true。どんなアイデアがありますか?

これはLCD-panel.serviceです。

[Unit]
Description=LCD Panel

[Service]
Type=oneshot
RemainAfterExit=true
User=root
WorkingDirectory=/path
ExecStart=/path/start-lcd-panel.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=lcd-panel

[Install]
#WantedBy=multi-user.target

これはPythonスクリプトを実行するstart-lcd-panel.sh bashスクリプトです。

#!/bin/bash
Application=LCD-Panel

cd /path

if ! [ -e "/path2_exist" ] ; then

    #run lcd panel
    sudo /path/run-menu.py lcd &
fi

if (( $(pgrep -f -c 'run-menu.py lcd') >= 1 )); then
    echo "Started LCD display"
fi

ベストアンサー1

長すぎると、次のように要約されます。サービスの場合、simpleプロセスはフォアグラウンドにあり、ブロックされることを望みます。これは提供されますExecStart

背景

マンページを読んでみると、次のような内容が目立つ。

単純さ:あなたのプロセスは実行されるためではthe service manager consider the unit started immediately after the main service process has been forked off ありません。単純さの私の解釈は、あなたのプロセスがフォアグラウンドでブロックされることを望むということです。It is expected that process configured with ExecStart= is the main of the servicerun-meny.py

oneshot:the manager will consider the unit up after the main exitsあなたの場合、start-lcd-panel.sh分岐後に終了する基本的なプロセスですrun-menu.py(行の末尾にアンパサンドがあります)。run-menu.pyフォークがどのように解釈されるのか、子プロセス(run-menu.py)がメインプロセスと一緒に死んでいるのかわかりません。

oneshot:結果をNote that if this option is used without RemainAfterExit= the service will never enter "active" unit state, but directly transition from "activating" to "deactivating" or "dead" since no process is configured that shall run continuously説明します。systemctl status

マニュアルページも興味深いです。Also note it is generally not to use idle or oneshot for long-running services.

**

私のアドバイス:私はスクリプトの使い方が表面的だと思います。なぜなら、スクリプトがWorkingDirectoryあなたが持っているパスである/ pathに切り替えるからですUser=root。これはsudoスクリプトの.inが重複していることを意味します。echo画面が点灯していないのを見ると、結局のところ利点が何であるかわかりません。

逆に:ExecStart=/path/run-menu.py lcdsimple

システムマンページ

おすすめ記事