1

ユニットファイル

[root@myserver system]# cat /etc/systemd/system/thiru.service
[Unit]
Description=My service

[Service]
Type=forking
PIDFile=/var/run/thiru.pid
ExecStart=/tmp/thiru.sh start
ExecStop=/tmp/thiru.sh stop

私のスクリプト

[root@myserver system]# cat /tmp/thiru.sh
#!/bin/sh

loop()
{
    while true
    do
        sleep 5
    done
}

if [ "$1" = "start" ]; then
    loop &
    echo "$!" > /var/run/thiru.pid
fi

if [ "$1" = "stop" ]; then
    kill $(cat /var/run/thiru.pid)
    rm -f /var/run/thiru.pid
fi

今はうまくいきますsystemctl start thiru.service。しかし、スクリプトを直接呼び出してサービスを開始すると/tmp/thiru.sh start、systemctl はそれを検出しません。

[root@myserver system]# /tmp/thiru.sh start
[root@myserver system]# systemctl status thiru.service
thiru.service - My service
   Loaded: loaded (/etc/systemd/system/thiru.service; static)
   Active: inactive (dead)

Jul 27 04:14:08 myserver systemd[1]: Starting My service...
Jul 27 04:14:08 myserver systemd[1]: Started My service.
Jul 27 04:14:17 myserver systemd[1]: Stopping My service...
Jul 27 04:14:17 myserver systemd[1]: Stopped My service.

私のサービスが開始されたことをsystemdに検出させる方法はありますか? おそらくPIDファイルを使用していますか?

4

1 に答える 1

1

systemd は、それ自体が起動したプロセスのみを監視します。

スクリプトに特定の開始/停止関数を記述する必要はもうありません。でスクリプトの主要部分を呼び出すだけExecStart=です。

PID ファイルはもう必要ありません。systemd は、cgroup を使用して、スクリプトによって生成されたすべてのプロセスを追跡できます。systemctl stop thiruすべてのプロセスを強制終了しない場合は、それsystemctl kill --signal=term thiruを実行します。

于 2015-08-08T15:33:58.563 に答える