I am running a program in the terminal that I can't escape with Ctrl–C and that I want to kill. How can I find its PID?
Command-Line – How to Find the Process ID (PID) of a Running Terminal Program
command lineprocess
command lineprocess
I am running a program in the terminal that I can't escape with Ctrl–C and that I want to kill. How can I find its PID?
Best Answer
Open another terminal and run
ps ax | grep foo
where foo is the name of the unresponsive program. This should return a line of output that looks something like this:The first field of each line of output is a number which represents the Process ID of the program matched by
grep
(you can safely ignore the last one, which representsgrep
itself.To halt the offending process, do:
kill pid
where pid is the Process ID of the program. You might have to use your judgment as to which of the matches needs to bekill
ed, or you could usetop
instead. Usingkill
by itself sends SIGTERM, which you should try first as it allows the program to properly clean up after itself. If SIGTERM fails, try SIGHUP, which is stonger medicine:kill -HUP pid
. If all else fails, send SIGKILL. But, you should only do so as a last resort, because SIGKILL causes the kernel to terminate the process immediately with no possibility for cleanup. This can at times result in data corruption or other problems. So again, only send SIGKILL as a last resort. To do so, dokill -KILL pid
orkill -9 pid
.If you are running a graphical interface, of course, you don't have to fool with this crazy command-line stuff to get the job done. Just open "System Monitor", navigate to the Processes tab, choose the process you want to halt (Hm, could it be the one using 90% CPU?) and right-click it. Since the process is already stopped, (that's the problem, right?) choose End Process or Kill Process from the resulting menu.