Script to continuously follow kubectl get pods
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you are debugging a rollout or watching a failing deployment, running kubectl get pods once is rarely enough. Pod status changes quickly, and retyping the command every few seconds is inefficient.
The good news is that Kubernetes already gives you built-in ways to watch pod state. In most cases, you do not need a complicated script at all; a watch-based command or a small shell loop is enough.
Use kubectl --watch First
The simplest solution is the built-in watch flag.
This keeps the command open and streams updates as pods are added, deleted, or change state. It is usually the right answer when you want a live view of pod status without writing any scripting logic.
You can scope it to a namespace too:
If you only care about a subset of pods, combine it with a label selector:
That reduces noise and makes the output more useful during focused troubleshooting.
watch Command Versus --watch
Some developers reach for the shell watch utility:
This reruns the whole command every two seconds and redraws the screen. It is different from kubectl --watch, which listens for changes from the Kubernetes API and streams them incrementally.
A rough guideline is:
- use
kubectl --watchfor native event-driven updates - use shell
watchwhen you want periodic redraws or need to combine multiple commands
Both are valid, but the built-in watch behavior is usually more efficient and more natural for pod status monitoring.
A Small Shell Loop When You Need Formatting Control
Sometimes you want a custom screen layout, timestamps, or a clear separator between refreshes. In that case, a shell loop is reasonable.
This is not event-driven like --watch, but it gives you full control over presentation. It is useful when you want a dashboard-style view in a terminal.
Watching Specific Columns
For quick operational checks, custom columns can make the output much easier to scan.
This avoids the default table when you only care about a few fields. It is especially useful in a busy namespace where the normal output has more columns than you need.
When You Need More Than Pod State
If the real goal is understanding why pods keep restarting or never become ready, kubectl get pods is only part of the picture. Pair it with other commands:
A pod watch tells you that something changed. Logs and events usually tell you why.
Example Helper Script
Here is a more practical wrapper that accepts a namespace and optional selector:
Usage:
This keeps the script small while still making repeated debugging sessions faster.
Common Pitfalls
One common mistake is using a polling loop when kubectl --watch would already solve the problem more cleanly. Polling is fine, but it adds noise and repeated API calls.
Another issue is forgetting namespace scope. If the command appears to show nothing useful, you may be watching the default namespace while the workload lives somewhere else.
It is also easy to confuse kubectl get pods --watch with log streaming. Pod state updates and container logs are different streams. If you want application output, use kubectl logs --follow instead.
Finally, keep permissions in mind. A script can only watch resources that the current kubeconfig user is allowed to read.
Summary
- The simplest way to continuously follow pod state is
kubectl get pods --watch. - Use namespace and label filters to reduce output and focus on the pods you actually care about.
- The shell
watchutility is useful for periodic redraws, but it is different from Kubernetes watch streaming. - A small shell loop is reasonable when you want timestamps or a custom terminal layout.
- For root-cause analysis, combine pod watching with
describe,logs --follow, and events.

