-2 PIDを終了してプロセスを再起動するスクリプト

-2 PIDを終了してプロセスを再起動するスクリプト

"restart.sh"スクリプトの実行に問題があります。主な目的は、IDでプロセスを終了してからプロセスを再開することです。

プロセス名には以下のように「DND」があります。

$ ps -ef | grep DND
root   18425     1 60 11:53 pts/0    00:40:34 java -jar DND_Retry.xml

注:PIDはスクリプトが実行されるたびに変更されます。

1 スクリプトの一部は次のとおりです。

  echo "Killing BND Process"

pids=$(pgrep -f BND)
for pid in "${pids}"; do
  if [[ $pid != $$ ]]; then
    kill -2 "$pid"
  fi
done

sleep 2

while true
do
shut1=`more /Data/v3/nohup_bnd.out |  grep  "has been shutdown" | wc -l`
        if [ $shut1 -eq 1 ]; then

    echo "process ended now restart"

問題は、スクリプトが最初は正常に実行されますが、再実行するとプロセスが終了しないことです。ただし、Kill -9を使用してプロセスを手動で終了し、スクリプトを再実行すると正常に実行され、目的の結果が生成されます。

上記のスクリプト/条件で修正する必要がありますか? Kill -2を実行する必要があります。

ベストアンサー1

複数のPIDを実行している場合は、引用された変数が"${pids}"問題を引き起こす可能性があります。引用符を削除する必要があります。

例:

$ pids=$(pgrep -f a_program_started_three_times)
$ for pid in "$pids"; do echo "pid is $pid"; done
pid is 563
564
565
$ for pid in $pids; do echo "pid is $pid"; done
pid is 563
pid is 564
pid is 565

任意に選択できる:

forループに追加のチェックを追加します。

pids=$(pgrep -f BND)
# if pid(s) were found, then...
if [ $? -eq 0 ]; then
    # removed the quotes from "${pids}" here
    for pid in $pids; do
        if [[ $pid != $$ ]]; then
            kill -2 "$pid"
        fi
    done
fi

おすすめ記事