CMD Master
Back to Blog
UpdatedArnošt Havelka

How to Find and Stop a Stuck Process Safely in Bash

Confirm a process and its PID, send SIGTERM first, verify the result, and reserve SIGKILL for a process that will not stop cleanly.

Start Interactive Lesson
How to Find and Stop a Stuck Process Safely in Bash

The safe way to stop a stuck process is to identify it, confirm its fresh process ID (PID), send the normal TERM signal, and check whether it exited. Use KILL only when graceful shutdown failed. The process-termination lesson provides a controlled process for practising the command before you use it on a real system.

Confirm the process before sending a signal

Start with a process listing rather than a PID copied from an old message:

ps -ef

Read the PID together with the owner and complete command. If you already have a candidate such as PID 123, narrow the output:

ps -p 123 -o pid,ppid,user,stat,etime,command

Replace 123 with the PID you just observed. Process IDs can be reused after a program exits, so the number alone is not durable evidence. A service may also have several worker processes with similar names; confirm which one is actually stuck.

Terminal
~$kill 3456

Ask the process to stop cleanly

After confirming a positive PID, send TERM explicitly:

kill -TERM 123

In Bash, plain kill 123 also sends TERM by default. The signal asks the program to shut down and gives it a chance to close files, finish writes, and run cleanup handlers. Wait briefly, then check the same PID again:

ps -p 123 -o pid,user,stat,command

No row usually means the process exited. A remaining row means it is still present, but the reason may be slow cleanup, blocked I/O, a stopped process, or a supervisor that started a replacement. Check logs and service status before escalating.

Use SIGKILL only as the last step

If the original process still exists, ignores TERM, and you are certain it is the right target, send:

kill -KILL 123

KILL cannot be caught or ignored. The operating system stops the process without letting the application clean up, so partial writes, temporary files, or abandoned child processes may remain. It can end the immediate process but cannot prove that the underlying problem is fixed.

Use a positive PID. Values such as 0, -1, or another negative number can address a process group or a much wider set of processes. Avoid broad copied commands such as pkill patterns until you understand exactly what they match.

Handle permission errors and restarting services

“Operation not permitted” normally means the process belongs to another account or is protected by system policy. Recheck the owner before considering sudo. Administrator privileges make a correct decision possible; they do not make an uncertain PID safe.

If the process immediately returns, a service manager or container orchestrator may be restarting it. Inspect that manager and the application logs instead of repeatedly killing replacement processes.

Knowledge Check

1 / 1

What should you verify immediately before sending a signal?

Use the process-list guide when you need more help reading ps output. Bash documents the default signal and accepted PID forms in its job-control builtins reference.

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

Process Guard Drill

Filter and manage processes.