sudo kill: Operation not permitted over SSH Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of sudo kill: Operation not permitted over SSH without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Using heredoc to execute multi-line commands on a remote machine. Trying to get pid of a server to kill that server located in the remote machine

#!/bin/bash

HOST_IP="10.180.5.23"
read -p "For HOST RESTART press 1" num
if [ "$num" == "1" ]
then
    ssh -t -t $HOST_IP << 'EOSSH'
    line=$(pgrep -f host_server1)
    echo $line
    arr=($line)
    sudo kill -9 "${arr[1]}"
EOSSH 
fi

Error : kill: (15015) – Operation not permitted

Answer

Since you have pgrep it is highly likely that you also have pkill.

Using pgrep and then kill introduces a theoretical race condition. Using pkill removes the race condition (from the script), and also avoids having to use a variable to hold the PIDs:

ssh -t -t "$HOST_IP" pkill -f host_server1

or, if you wanted to send the KILL signal:

ssh -t -t "$HOST_IP" pkill -KILL -f host_server1

Note that with -f, pgrep and pkill will match the pattern against the whole command line, not just the process name.

With -n and -o, the newest or the oldest (respectively) of the matching processes will be affected. Thus, if you want to KILL the newest host_server1 and nothing else:

ssh -t -t "$HOST_IP" pkill -KILL -n -f host_server1
We are here to answer your question about sudo kill: Operation not permitted over SSH - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji