1

A: 開いているソケットを保持するプログラム B: サービスとして実行されているウォッチドッグ スクリプト:

while true
do
        if [ -z "`pidofproc $1`" ]; then
                $1;
            chrt -f -p 40 `pidofproc $1`
                sleep 8
        fi;
        sleep 2
done

サービスが開始されたとき - サービスが停止したときにウォッチドッグが開始さ
れました - ウォッチドッグとプログラムが強制終了されます (killall)。

現在、プログラムは自分自身をアップグレードしたいので、呼び出しますsystem( "upgrade.sh" );

upgrade.sh:

/sbin/service watchdog stop

.... install upgrade  .....

exec /sbin/service watchdog start &

アップグレードは正常に実行されましたが、プログラムの起動時に - ソケットを開けません (既に使用されています) - このエラーで - プログラムは終了します (ウォッチドッグによって再起動されます)。

lsof -i は、ポート上の 3 つのプログラムを表示します。

watchdog

program

sleep

プログラムとスリープ pid は常に変更されます (つまり、終了/再起動動作)

ウォッチドッグ pid パーシスタント。

を に置き換えてみましsystem(...)

if(!fork()) exec(...)、しかし同じ問題が残っています。

4

1 に答える 1

0

Depending on how fast the restart happens after the shut down, the socket will be lingering around. Linux, by default, keeps sockets marked as in use for some time after they've been released (either by close() or when the process dies) to make sure that incoming connection attempts or data which is late due to network latencies won't end up at the wrong application.

This has to be fixed inside the application. It is required to set the SO_REUSEADDR sockopt. As per the manpage of socket(7):

Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For AF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. When the listening socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this port for any local address. Argument is an integer boolean flag.

This has to be set using setsockopt after the socket was created.

于 2012-12-13T16:36:00.250 に答える