如何获取进程的pid并在shell脚本中对其调用kill -9?

dav*_*vid 9 linux shell bash

我有一个应用程序服务器,我们通过运行下面的命令开始,只要我们运行下面的命令,它就会启动我的应用程序服务器

/opt/data/bin/test_start_stop.sh start
Run Code Online (Sandbox Code Playgroud)

为了停止我的应用程序服务器,我们这样做 -

/opt/data/bin/test_start_stop.sh stop
Run Code Online (Sandbox Code Playgroud)

因此,只要我的应用程序服务器正在运行,如果我运行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
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试制作一个 shell 脚本,我需要在其中停止我的应用程序服务器,然后我将在停止服务器后检查我的进程是否正在运行,如果我的进程仍在运行,那么我需要执行 kill -9我的应用程序pid。而且我不确定如何在 shell 脚本中获取我的进程的 pid 然后杀死它。

下面是我现在拥有的 shell 脚本,它将关闭我的应用程序服务器,然后检查我的进程是否仍在运行,如果它仍在运行,那么我需要获取进程的 pid 和在那个pid上执行kill -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
Run Code Online (Sandbox Code Playgroud)

如何获取我的进程的 pid 并在上面的 shell 脚本中执行 kill -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
Run Code Online (Sandbox Code Playgroud)

arn*_*efm 12

首先; “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."
Run Code Online (Sandbox Code Playgroud)