Error message when killing process in bash

40 Views Asked by At

Linux version: Linux <server_alias> 3.10.0-1160.76.1.el7.x86_64 #1 SMP Tue Jul 26 14:15:37 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

I have the following function :

killProc () {
    local PROCNAME="$1"

    echo "$(date) | Attempting to shutdown $PROCNAME ... "

    if [[ -z $(findproc "$PROCNAME") ]]; then
        echo "Process not running ..."
    else
        #search for PID based on process name
        local PROCESS_ID=$( ps -ef -u ${USER} | grep "${APPNAME}" | grep -v grep | grep "procname ${PROCNAME}" | awk '{print $2}')

        #only if this search returns a result do we kill the process
        if [[ "$PROCESS_ID" ]]; then
            set -x 
            kill -9 "$PROCESS_ID"

            #check that process has been killed
            temp_exit=$?

            if [ $temp_exit -eq 0 ]; then
                while kill -0 ${PROCESS_ID} 2>/dev/null; do
                    echo "$(date) | Waiting for process to stop running ... "
                    sleep 2
                done 

                echo "Done"
            else
                echo "Failed"
            fi
        set +x 
        else
            echo "Missing"
        fi
    fi
}

Function is meant to kill a process using it's procname The output from ps is something like this:

<USER>       7456  0.3  0.0  77112  9268 pts/0    S    16:29   0:00 <path> -proctype dis -procname procname1 <params> -p 10000

For most procs, it all works well, but for some I get an error

Wed Mar 20 16:20:13 UTC 2024 | Attempting to shutdown procname1 ...
+ kill -9 6603
+ temp_exit=0
+ '[' 0 -eq 0 ']'
+ kill -0 6603
++ date
+ echo 'Wed Mar 20 16:20:13 UTC 2024 | Waiting for process to stop running ... '
Wed Mar 20 16:20:13 UTC 2024 | Waiting for process to stop running ...
+ sleep 2
<path>/bin/killProcs.sh: line 26:  6603 Killed                  taskset -c 39,40 <path> <params> -procname procname1 -p 10001 < /dev/null > ${LOG}/dis_test_nont_$(date +%s).log 2>&1
+ kill -0 6603
+ echo Done
Done

That bit after the sleep 2. I wouldn't expect that to show up. Line 26 is the entire function.

I tried using kill -9 "$PROCESS_ID" 2> /dev/null but no success.

I tried removing all the code in the function and sometimes I get it, sometimes I don't.

Any suggestions ?

UPDATE: I have a few other processes which are killed by this same function and they don't have the same issue.

0

There are 0 best solutions below