Yocto Linuxサービススクリプトの問題

Yocto Linuxサービススクリプトの問題

ディレクトリにmqtt.serviceシステムスクリプトを作成しました。/home/root

これは私のmqtt.serviceスクリプトです。

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
[Unit]
Description=mqtt

DefaultDependencies=no
After=xdk-daemon.service


[Service]

Restart=always
RestartSec=10
ExecStart=/bin/sh /home/root/mosquitto-1.4/mqtt_start.sh



[Install]

WantedBy=multi-user.target

これが内容ですmqtt_start.sh

#!/bin/sh
/home/root/mosquitto-1.4/src/mosquitto -c /home/root/mosquitto-1.4/mosquitto.conf -d -p 1885 > /dev/null 2>&1

この行を貼り付けると:

/home/root/mosquitto-1.4/src/mosquitto -c /home/root/mosquitto-1.4/mosquitto.conf -d -p 1885

端末に移動すると、mqtt ブローカーは正常に起動しますが、ここでは起動できません。

私がやった

$ systemctl enable /home/root/mqtt.service
$ systemctl status mqtt.service
● mqtt.service - mqtt
   Loaded: loaded (/home/root/mqtt.service; enabled)
   Active: failed (Result: resources) since Mon 2015-04-06 10:42:48 UTC; 24s ago
 Main PID: 677 (code=exited, status=0/SUCCESS)

Apr 06 10:42:39 edison sh[677]: 1428316959: Warning: Mosquitto should not be run as root/administrator.
Apr 06 10:42:48 edison systemd[1]: mqtt.service holdoff time over, scheduling restart.
Apr 06 10:42:48 edison systemd[1]: mqtt.service failed to schedule restart job: Unit mqtt.service failed to load: No such file or directory.
Apr 06 10:42:48 edison systemd[1]: Unit mqtt.service entered failed state.

どこで間違っているのか教えてください。

編集する:

再起動後、私は次のことをしました。

systemctl 状態 mqtt.service結果は次のとおりです。

● mqtt.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

ベストアンサー1

システムサービスユニットのデフォルト値はですが、Type=simpleラッパースクリプトはその--daemonオプションを使用しています。これは準備プロトコルの不一致です。準備プロトコルの不一致により、サービスが正しく開始されないか(より一般的に)systemdでエラーと診断される可能性があります。

後者はここで起こっています。システムを過度にエンジニアリングしたため、デーモンは不必要に分岐しました。2回以上、間違ったプロセスは実際のデーモンプロセスです。また、ラッパースクリプトでsystemdサービスユニット機能を不必要に複製しています。

ラッパーシェルスクリプトを完全に削除し、次のようにサービスユニットを作成します。

[単位]
説明=モスキートMQTTブローカー
ドキュメント=man:mosquitto(8)
ドキュメント=man:mosquitto.conf(5)
ConditionPathExists=/home/root/mosquitto-1.4/mosquitto.conf
以降=xdk-daemon.service

[提供する]
ExecStart=/home/root/mosquitto-1.4/src/mosquitto -p 1885 -c /home/root/mosquitto-1.4/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
ユーザー=蚊
再開=失敗した場合
再起動秒 = 10

[インストールする]
WantedBy =マルチユーザー。ターゲット

注:

  • このオプションは必要ありません--daemon。ブローカーデーモン化されましたsystemdが実行するとき。
  • このオプションは使用しないでください--daemon。 Mosquittoはforking準備プロトコルについて話しません。実はまだ準備ができていません。分岐後に終了します。 (いつか、人々はこのようなプログラムを書いてリスニングソケットを継承します。したがって、ソケットを介してアクティブにすることができます。 )
  • ラッパースクリプトでstdoutとエラーを操作する必要はありません。 systemd はデーモンの標準出力とエラーを処理します。必要に応じてサービス単位ファイルで構成できますが、このプログラムではまったく必要ないようです。
  • systemdが認識する正しいプロセスはデーモンなので、systemctl reload mqtt.service正しい位置と操作でシグナルを送信できるようになりました。
  • systemd自体はユーザーとしてプロキシを呼び出すので、その名前のmosquittoユーザーアカウントが存在し、必要なアクセス権があることを確認する必要があります。ログの警告を参照してください。
  • on-failure再起動戦略としてsystemdの人々がお勧めしますalways
  • DefaultDependenciesデーモンプロセス以外ではfalseに設定しないでください。実際に動作します一般に、開始プロセスの初期および終了プロセスの後半のほとんどの状況ではそうではありません。基本サービス単位の依存関係適切このデーモンの場合。

おすすめ記事