180

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?

Jjed
  • 13,874

11 Answers11

183

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:

$ ps ax | grep firefox
2222 ?        S      0:00 /bin/sh /usr/lib/firefox-3.6.9/firefox
2231 ?        Sl   514:36 /usr/lib/firefox-3.6.9/firefox-bin
30290 pts/2    S+     0:00 grep --color=auto firefox

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 represents grep 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 be killed, or you could use top instead. Using kill 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, do kill -KILL pid or kill -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.

Credit to koanhead

Jjed
  • 13,874
  • 9
    People should never use SIGKILL (kill -9) unless all other methods have failed. Only listing kill -9 is bad advice. I've edited the answer to correct it. – Scott Severance Aug 26 '12 at 10:04
  • How can I find the pid of a custom function. I have a function that contains a loop with a ping command and sleep. According to https://unix.stackexchange.com/a/275826/72988 , it is not possible in Ubuntu without multiplexer. – Timo Feb 03 '18 at 09:27
  • To get all open programs by id would not be possible because then you have also all services and end with ps -ef? – Timo Jun 20 '21 at 07:35
82

I don't think there is any need of such long commands when you can accomplish the same commands with pgrep, pkill, pidof etc...

To get the PID of a running program you can use commands like pgrep or pidof:

pgrep

pgrep [options] pattern

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. All the criteria have to match. For example,

  • To find process named sshd owned by root.

    $ pgrep -u root sshd
    
  • List the processes owned by root OR daemon.

    $ pgrep -u root,daemon

  • List the processes searching the full command line (-f) and list the full command line as well as the process ID (-a).

    $ pgrep -fa needle
    

pidof

pidof finds the process id's (pids) of the named programs. It prints those id's on the standard output.

pidof program_name

To kill a program by pid use pkill. Example:

pkill pid    
pkill -f process_name    
pkill -o process_name    
pkill -n process_name    
pkill -l process_name
  • -f flag: Searches the process_name (see man pkill)
  • -o flag: Select only the oldest of the matching processes.
  • -n flag: Select only the newest of the matching processes.
  • -l flag: List the process name as well as the process ID.
Pablo Bianchi
  • 15,657
32

The easiest way to know the pid of a running program is using:

pidof <application name>

For example, if you started vim and want to know its pid:

pidof vim

Remember that you will need to provide the exact program name that has been started.

For example, if you are running vi and execute pidof vim you won't get correct results.

Refer to pidof's manual page for more info.

jobin
  • 27,708
  • This isn't installed by default. It might be in the sysvinit-utils package. – flickerfly Sep 19 '14 at 19:24
  • I got MariaDB. pidof mysqld returns a PID. However, if I type pidof Mariadb nothing is returned. Is it the same PID for both MariaDB and MySQL? – Pathros Jun 13 '18 at 15:18
30

I have found it is nice to use a case insensitive search by adding the "-i" and using "aux" instead of "ax" to get a more descriptive output:

ps aux | grep -i firefox

If you would like to kill all the processes you may use:

ps aux | grep -i firefox | awk {'print $2'} | xargs kill -9

That is a forceful kill. Drop the "-9" if you want a soft kill.

John Foley
  • 499
  • 5
  • 6
8

If you want only the pid (useful for autokill scripts, etc...)

pgrep -f desired_program_name
MikeL
  • 181
2

For those running Ubuntu 16.04 LTS, you must use ps ax and not just ps at the command line, otherwise you will just get the processes spawning from the particular terminal instance that you're in. (usually just 'bash' [the shell you're in] and 'ps' [what you just ran])

cygnus_x1
  • 102
1

To get PIDs for all running services, you can use this command:

pstree -p -a
Error404
  • 7,440
0
top

or

screen -list|ls

list all pid with corresponding process

kill -[pid]
Rakly3
  • 1
0

sed (stream editor) for process id(s) or pgrep:

echo $(p="bash"; ps -e | sed -E "s/^ +([[:digit:]]+).+${p}$/# \1/;s/^[^#].+$//;/^$/d;" -)
0

You can also open another terminal (or switch to another tty) and run top, which is basically a text version of the System Monitor. The first column lists the PID of each running process, which you can kill by pressing K, entering the PID and then entering a numeric signal to send.

0

You could do:

ps ax | grep firefox | cut -f2 -d" " - | xargs kill -9

first two pipes get process info, next we try to get PID column by using the old-school cut and then we give the resulting PID to kill.

arianvc
  • 1
  • 1
  • 1
    kill -9 is the biggest gun you have to kill a process. You should try a more gentle signal at first before resorting to SIGKILL to give the process a chance to clean up its resources. – Byte Commander Sep 27 '16 at 21:30
  • That's true @ByteCommander. I wanted to make it a one liner that will get the job done. – arianvc Sep 28 '16 at 06:40