← Writing
DevOps

Killing Processes in K8s Pods

Publication Date: 12 December 2025
Author: Ailven LIU 刘翔
All Rights Reserved

A kill -9 that answers No such process for a PID that ps has just printed is one of the more confusing failures a container will hand you. The process plainly exists and is plainly visible. The signal simply never arrives. Almost every instance of this comes down to a PID namespace boundary between the shell issuing the signal and the process meant to receive it.

Problem

Process visible in ps -ef but cannot be killed:

$ ps -ef | grep <process>
root  3563929  ...  /bin/bash ./script.sh

$ kill -9 3563929
-bash: kill: (3563929) - No such process

Root cause

The target runs in a different PID namespace from the shell. Namespaces partition the PID space, so a number that is valid inside one namespace does not resolve to the same process in another — or necessarily to any process at all. The kernel reports No such process because, from the shell's own namespace, that number really is unassigned. Nothing is wrong with the process, and nothing is wrong with kill; the two are simply not in the same numbering scheme.

Two commands confirm it:

# 1. Check if PID namespaces differ
readlink /proc/<PID>/ns/pid
readlink /proc/$$/ns/pid
# Different output = namespace mismatch

# 2. Find the process's PID within its own namespace
cat /proc/<PID>/status | grep NSpid
# Output: NSpid: <host_PID> <container_PID>

NSpid lists the process's PID in each namespace it belongs to, outermost first. The second number is the one a kill issued from inside the container would accept.

Solution

Use nsenter to enter the target's namespace and signal it with the container-local PID:

# Get container PID from NSpid (second number)
CONTAINER_PID=$(cat /proc/<HOST_PID>/status | grep NSpid | awk '{print $3}')

# Enter namespace and kill
nsenter -t <HOST_PID> -p -m kill -9 $CONTAINER_PID

For a process whose NSpid reads 3563929 5984, that becomes:

nsenter -t 3563929 -p -m kill -9 5984

Generalising the fix

Three cases account for most unkillable processes: the process is already gone, it is a zombie, or its namespace differs from yours. The script below tests them in that order and dispatches accordingly.

Issue Diagnosis Solution
PID namespace mismatch Different namespace IDs nsenter -t <PID> -p -m kill <container_PID>
Zombie process State = Z in ps aux Kill parent: kill -9 <PPid>
Process already terminated /proc/<PID> missing No action needed
#!/bin/bash
TARGET_PID=$1

# Check if process exists
[ ! -d "/proc/$TARGET_PID" ] && echo "Process not found" && exit 1

# Check if zombie process
STATE=$(grep "^State:" /proc/$TARGET_PID/status | awk '{print $2}')
if [ "$STATE" = "Z" ]; then
    PPID=$(grep "^PPid:" /proc/$TARGET_PID/status | awk '{print $2}')
    echo "Zombie process - killing parent: $PPID"
    kill -9 $PPID
    exit 0
fi

# Check PID namespace
PROC_NS=$(readlink /proc/$TARGET_PID/ns/pid)
CURR_NS=$(readlink /proc/$$/ns/pid)

if [ "$PROC_NS" != "$CURR_NS" ]; then
    echo "PID namespace mismatch - using nsenter"
    CONTAINER_PID=$(grep "^NSpid:" /proc/$TARGET_PID/status | awk '{print $3}')
    nsenter -t $TARGET_PID -p -m kill -9 $CONTAINER_PID
else
    kill -9 $TARGET_PID
fi

Where the pod is reachable, two simpler routes exist:

# From within the pod
kubectl exec -it <pod-name> -- kill <PID>

# Terminate the entire pod
kubectl delete pod <pod-name> --force --grace-period=0

The first is preferable when one process is at fault, since it keeps the rest of the pod alive. The second takes every process in the pod with it and belongs at the end of the list, not the start.