システムサービスによって開始されたプロセスをアクティブに保つのに問題があります。

システムサービスによって開始されたプロセスをアクティブに保つのに問題があります。

最初のサービスが期待どおりに機能するように努めていますsystemd。これが私が持っているものです:

gateway-watchdog.service:

# A systemd service for the Gateway Watchdog

[Unit]
Description=gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running

[Service]
Type=simple
User=root
WorkingDirectory=/home/ubuntu/lora_gateway/
ExecStart=/home/ubuntu/lora_gateway/watchdog.sh

[Install]
WantedBy=multi-user.target

gateway-watchdog.timer:

[Unit]
Description=gateway-watchdog: Timer to run the associated gateway-watchdog service

[Timer]
# Run service 1 minute after boot
OnBootSec=1min
# Run service 15 minutes after the last run
OnUnitActiveSec=30s

[Install]
WantedBy=timers.target

watchdog.sh次の行があります(一部のifステートメントに含まれています)。

#!/bin/bash
...
if [[condition]]; then
    command="python3 gateway.py"
        # Process needs to be run in the background with '&'. If it isn't, the systemd service will treat this script
        # as still running and so not re-run it at the time specified by the timer service.
    echo "2 $(date +'%F %T'): Running: $command" >> $LOG_FILE_PATH
    eval "$command"
        # Command must be run using eval. If it isn't, the ampersand at the end of the command is effectively ignored
    exit
fi

私は必要です:

サービスが実行されますwatchdog.shifスクリプトの条件が true の場合は、別のプロセスで開始して終了し、そのサービスはもうアクティブとは見なされなくなり、30 秒後に再実行されますwatchdog.shpython3 gateway.py

さまざまなサービスをさまざまに組み合わせて(、およびを試しTypeました)、および/またはコマンドラインの最後に、および/または両方を使用します。simpleoneshotforking&ExecStartnohupgateway.py

アクティブのままにするとpython3 gateway.py常にサービスの一部になるため、CGroupサービスは維持されますactive (running)

私が望むことを達成するためにこの行動をどのように変えることができますか?どんなアイデアでも大いに感謝します。

編集する:Uの提案に従って変更された内容を反映するために、上記のファイルの内容を更新しました。

上記の文書に基づいて、sudo systemctl status gateway-watchdog.service以下を提供します。

● gateway-watchdog.service - gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
     Loaded: loaded (/etc/systemd/system/gateway-watchdog.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-01-13 01:45:25 UTC; 8s ago
TriggeredBy: ● gateway-watchdog.timer
   Main PID: 30684 (watchdog.sh)
      Tasks: 3 (limit: 4435)
     CGroup: /system.slice/gateway-watchdog.service
             ├─30684 /bin/bash /home/ubuntu/lora_gateway/watchdog.sh
             └─30696 python3 gateway.py

Jan 13 01:45:25 ubuntu systemd[1]: Started gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running.

良い:gateway.pyプロセスはまだ実行中です。エラー:以前の実行はまだアクティブなため、サービスは30秒間再実行されません。

&back を入れると次のようcommand="python3 gateway.py &"sudo systemctl status gateway-watchdog.serviceなります。

● gateway-watchdog.service - gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
     Loaded: loaded (/etc/systemd/system/gateway-watchdog.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Thu 2022-01-13 01:49:05 UTC; 6s ago
TriggeredBy: ● gateway-watchdog.timer
    Process: 33724 ExecStart=/home/ubuntu/lora_gateway/watchdog.sh (code=exited, status=0/SUCCESS)
   Main PID: 33724 (code=exited, status=0/SUCCESS)

Jan 13 01:49:05 ubuntu systemd[1]: Started gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running.
Jan 13 01:49:05 ubuntu systemd[1]: gateway-watchdog.service: Succeeded.

良い:サービスは非アクティブなので、30秒後に再度実行されます。悪い:python3 gateway.pyプロセスはすぐに終了します。

ベストアンサー1

参考に解決方法を見つけましたhttps://stackoverflow.com/a/57041270/2909854。現在の行はcommand次のとおりですwatchdog.sh

command="systemd-run --scope -E setsid nohup python3 $GATEWAY_SCRIPT_PATH"

Typeまた、私のサービス設定をに変更してforking追加する必要がありましたTimeoutSec=1。タイムアウトが必要かどうかはわかりませんが、少なくともデバッグするのに役立ちます。

おすすめ記事