Skip to content
Commitpedia
Log in Join & edit

List Kubernetes pods that are not Running and Ready

A bash script that lists every pod across namespaces that is Pending, failing or running but not ready, with the waiting reason, last termination reason and restart count.

Edit history of List Kubernetes pods that are not Running and Ready: 1 recorded edit
Edit history · 1 edit
Type
Script
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Language
bash
Licence
CC0-1.0

kubectl get pods -A on a busy cluster prints hundreds of healthy lines. This script shows only the problems: pods that are not in the Running phase, and running pods where at least one container is not ready. For each one it adds the reason Kubernetes reports, such as CrashLoopBackOff, ImagePullBackOff or OOMKilled. It only reads from the cluster.

The script

#!/usr/bin/env bash
set -euo pipefail

usage() {
  echo "Usage: $(basename "$0") [-n NAMESPACE] [-l SELECTOR] [-c]" >&2
  echo "  -n  only this namespace (default: all namespaces)" >&2
  echo "  -l  label selector, for example app=web" >&2
  echo "  -c  also list Completed pods" >&2
  exit 2
}

SCOPE=(--all-namespaces)
SELECTOR=()
SHOW_COMPLETED=0

while getopts ":n:l:ch" opt; do
  case $opt in
    n) SCOPE=(--namespace "$OPTARG") ;;
    l) SELECTOR=(--selector "$OPTARG") ;;
    c) SHOW_COMPLETED=1 ;;
    *) usage ;;
  esac
done

command -v kubectl >/dev/null || { echo "kubectl not found" >&2; exit 1; }

kubectl get pods "${SCOPE[@]}" ${SELECTOR[@]+"${SELECTOR[@]}"} \
  -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,PHASE:.status.phase,READY:.status.containerStatuses[*].ready,WAITING:.status.containerStatuses[*].state.waiting.reason,LAST:.status.containerStatuses[*].lastState.terminated.reason,RESTARTS:.status.containerStatuses[*].restartCount,NODE:.spec.nodeName' \
  --no-headers |
awk -v show_completed="$SHOW_COMPLETED" '
  BEGIN { found = 0 }
  {
    phase = $3; ready = $4
    if (phase == "Succeeded" && show_completed == 0) next
    if (phase == "Running" && ready !~ /false/) next
    if (!found) {
      printf "%-20s %-45s %-10s %-12s %-28s %-14s %-9s %s\n", "NAMESPACE", "NAME", "PHASE", "READY", "WAITING", "LAST", "RESTARTS", "NODE"
      found = 1
    }
    printf "%-20s %-45s %-10s %-12s %-28s %-14s %-9s %s\n", $1, $2, $3, $4, $5, $6, $7, $8
  }
  END {
    if (!found) { print "All matching pods are Running and Ready." > "/dev/stderr"; exit 0 }
    exit 1
  }'

Usage

./k8s-not-running.sh
./k8s-not-running.sh -n shop
./k8s-not-running.sh -l app=web -c
./k8s-not-running.sh || echo "problems found"

The exit code is 0 when everything is healthy and 1 when at least one pod is listed, so the script can be used as a simple check in cron or CI.

Reading the columns

  • PHASE: the Pod phase: Pending (not scheduled yet or still pulling images), Running, Succeeded, Failed or Unknown (node not reporting).
  • READY: one true/false per container. A Running pod with false is failing its readiness probe or restarting.
  • WAITING: why a container is not running right now, for example CrashLoopBackOff, ErrImagePull, ImagePullBackOff or CreateContainerConfigError (often a missing ConfigMap or Secret key).
  • LAST: how the previous instance ended, for example OOMKilled or Error.
  • NODE: <none> means the scheduler has not placed the pod; look at its events for a FailedScheduling message.

Limitations

  • Columns that list several containers are comma-separated; for multi-container pods, check details with kubectl describe pod.
  • Init containers are not shown in the READY column. A pod stuck in initialization is still listed, because its phase stays Pending.
  • Pods that are being deleted can still show as Running for their grace period.

Official documentation

See also

  • 0139 Collect describe, events and logs of a failing Kubernetes pod A bash script that saves a pod's status, describe output, events, YAML, current and previous logs of every container and its node… Intermediate · How-to · Linux, macOS Script Troubleshooting 15 Oct 2026
  • 0140 kubectl cheat sheet: the commands you use every day A compact reference of everyday kubectl commands: contexts and namespaces, listing and inspecting objects, logs and exec, applying changes, rollouts, scaling and… Beginner · Reference · Any system Article Concepts · Troubleshooting 15 Oct 2026
  • 0142 Fix: ImagePullBackOff and ErrImagePull in Kubernetes The pod cannot start because the node failed to pull the container image. Read the "Failed to pull image" event to see… Intermediate · Fix · Linux Article Troubleshooting 15 Oct 2026
  • 0143 Fix: CrashLoopBackOff in Kubernetes The container starts, exits, and Kubernetes waits longer before each restart. Read the previous container's logs and its exit code to find… Intermediate · Fix · Linux Article Troubleshooting 15 Oct 2026
  • 0144 Fix: Pod stuck in Pending with “0/3 nodes are available: Insufficient cpu” The scheduler cannot find a node with enough unreserved CPU for the pod's request. Compare the request with node allocatable resources, then… Advanced · Fix · Linux Article Troubleshooting · Workloads & services 15 Oct 2026
  • 0145 Fix: OOMKilled (exit code 137) in Kubernetes A container used more memory than its limit and the kernel killed it. Confirm the reason, measure real usage, then raise the… Advanced · Fix · Linux Article Troubleshooting · Workloads & services 15 Oct 2026

Repos in the wild

  • derailed/k9s derailed/k9s K9s is a terminal UI for Kubernetes that lets you browse resources, read logs, open shells and follow cluster changes from the keyboard. GitHub

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. commitpedia-admin +3,690 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?