プロセスのpidを取得し、シェルスクリプトでそのプロセスに対してkill -9を呼び出すにはどうすればよいですか? [コピー]

プロセスのpidを取得し、シェルスクリプトでそのプロセスに対してkill -9を呼び出すにはどうすればよいですか? [コピー]

アプリケーションサーバーがあり、以下のコマンドを実行して起動します。以下のコマンドを実行すると、アプリケーションサーバーが起動します。

/opt/data/bin/test_start_stop.sh start

マイアプリケーションサーバーを停止するには:

/opt/data/bin/test_start_stop.sh stop

したがって、私のアプリケーションサーバーが実行されps auxたら、次を実行すると次のような結果が得られます。

user@machineA:/home/user$ ps aux | grep gld_http
user 20426  0.0  0.0   8114   912 pts/4    S+   13:33   0:00 grep gld_http
user 40509  0.1  0.9 3878120 1263552 ?     Sl   Sep22   1:53 /opt/data/bin/gld_http

次に、アプリケーションサーバーを停止する必要があるシェルスクリプトを作成しようとしています。その後、サーバーを停止してプロセスが実行中であることを確認し、プロセスがまだ実行中の場合は、kill -9 my application pidを実行する必要があります。 。そして、シェルスクリプトからプロセスのpidを取得してから終了する方法がわかりません。

以下は、私のアプリケーションサーバーをシャットダウンしてから、私のプロセスがまだ実行されていることを確認する現在持っているシェルスクリプトです。まだ実行中の場合は、私のプロセスのpidとそのpid -9を取得する必要があります。殺す。

#!/bin/bash

/opt/data/bin/test_start_stop.sh stop

if ps aux | grep -v "grep" | grep "gld_http"
then
    echo "Server is still running"
    # now get the pid of my gld_http process and invoke kill -9 on it
else
   echo "Server is stopped"
fi

上記のシェルスクリプトからプロセスのpidを取得し、それを-9で終了するにはどうすればよいですか?

直す:-

だから私の最後のスクリプトは次のようになります。

#!/bin/bash

/opt/data/bin/test_start_stop.sh stop

if ps aux | grep -v "grep" | grep "gld_http"
then
    echo "Server is still running"

    # Getting the PID of the process
    PID=`pgrep gld_http`

    # Number of seconds to wait before using "kill -9"
    WAIT_SECONDS=10

    # Counter to keep count of how many seconds have passed
    count=0

    while kill $PID > /dev/null
    do
        # Wait for one second
        sleep 1
        # Increment the second counter
        ((count++))

        # Has the process been killed? If so, exit the loop.
        if ! ps -p $PID > /dev/null ; then
            break
        fi

        # Have we exceeded $WAIT_SECONDS? If so, kill the process with "kill -9"
        # and exit the loop
        if [ $count -gt $WAIT_SECONDS ]; then
            kill -9 $PID
            break
        fi
    done
    echo "Process has been killed after $count seconds."    
else
   echo "Server is stopped"
fi

ベストアンサー1

まず、「kill -9」は最後の手段でなければなりません。

killこのコマンドを使用して、プロセスにさまざまな信号を送信できます。送信される基本信号killは15(SIGTERMと呼ばれる)です。プロセスは通常、この信号を捕捉してクリーンアップした後に終了します。 SIGKILL信号()を送信すると、-9プロセスはすぐに終了するしかありません。これにより、ファイルが破損し、状態ファイルと開いたソケットが残り、プロセスを再起動すると問題が発生する可能性があります。この-9信号はプロセスでは無視できません。

これが私がする方法です:

#!/bin/bash

# Getting the PID of the process
PID=`pgrep gld_http`

# Number of seconds to wait before using "kill -9"
WAIT_SECONDS=10

# Counter to keep count of how many seconds have passed
count=0

while kill $PID > /dev/null
do
    # Wait for one second
    sleep 1
    # Increment the second counter
    ((count++))

    # Has the process been killed? If so, exit the loop.
    if ! ps -p $PID > /dev/null ; then
        break
    fi

    # Have we exceeded $WAIT_SECONDS? If so, kill the process with "kill -9"
    # and exit the loop
    if [ $count -gt $WAIT_SECONDS ]; then
        kill -9 $PID
        break
    fi
done
echo "Process has been killed after $count seconds."

おすすめ記事