프로세스의 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."

답변2

pidof gld_http시스템에 설치되어 있으면 제대로 작동합니다.

man pidof설명하다:

Pidof  finds  the process id’s (pids) of the named programs. It prints those id’s on the standard output.

편집하다:

귀하의 응용 프로그램에 다음을 사용할 수 있습니다 command substitution.

kill -9 $(pidof gld_http)

@arnefm이 언급했듯이 kill -9최후의 수단으로 사용해야 합니다.

답변3

/proc/1234(알려진 pid) 프로세스가 실행 중인지 확인할 수도 있습니다 . 많은 애플리케이션은 중복 이름을 방지하기 위해 동일한 프로세스인지 확인하기 위해 참조용으로 /var/log에 pid를 저장합니다.

echo $! > /var/log/gld_http.pid프로그램을 호출한 후 즉시 스크립트에서 출력을 리디렉션하여 pid를 저장할 수 있습니다.

관련 정보