12

I want to exit a process using the command line, but I can't recall the command to look up the process's ID. Does anyone know it?

7 Answers 7

14

The command to find out a process's id (given its name) is pidof. However since your intention is to kill the process, there are better/easier ways than using pidof to find its pid first:

Assuming the process you want to kill is uniquely identified by its name (or you want to kill all the processes with that name), you don't need to know its pid. You can simply do killall processname.

If there are multiple processes with the same name, but you only want to kill one of them, using pidof won't help you much, because it will give you the pids, but it won't give you any information to determine which of the pids belongs to the process you actually want to kill.

In this case you can do ps aux | grep processname which will shows all processes with the given name as well as the console they're running on (if applicable) and the arguments they were invoked with, which hopefully allows you to identify the process you're looking for.

If the process opened any X-windows, you can also use xkill to kill (invoke xkill and then click on the window).

7

You should have a look at pgrep and pkill, two very handy and powerful utilities that are found on most Linux and UNIX systems.

$ apropos pgrep
pgrep (1) - look up or signal processes based on name and other attributes
5

You can run ps -A in the terminal to show all the processes (with their process ID) that are currently running.

1
  • Pipe to grep to search for name of specific process: ps -A | grep 'my name' Oct 22, 2019 at 3:08
2

I also had a similar problem. I used pstree -p; this showed me the current running processes including Process IDs.

1
  • interesting command
    – jumpnett
    Apr 26, 2013 at 15:34
2

My favorite is pstree -p | grep $(program_name). This actually greps the process, highlights it and shows the pid in parenthesis.

My next favorite (especially when programming and needing all pids from process) is pgrep -law "". This literally gives you every pid running followed by the application (with absolute path).

This can be reduced to pgrep -lw "" for pid followed by program name or even just pgrep -l for all pids

Of course, you can search for a particular program (or part of a program's) name. (i.e. pgrep -lw firef will probably display something like "4567 firefox").

2
  • Wouldn't pstree -psH $(pgrep prgram_name) be better for the first case?
    – muru
    Sep 18, 2014 at 21:57
  • Yeah that works, too. You could make an alias for that. I was just using the $( ) to represent a program's name; I think that typing $(pgrep name) is a little annoying because of the dollar sign, but I think an alias of that could be helpful.
    – dylnmc
    Sep 19, 2014 at 14:10
1

pidof. Issue man pidof for details.

1

This answer, from @Ben on a duplicated question, has solved my problem:

Consider using the top command in terminal.

   The  top  program  provides  a dynamic real-time view of a running
   system.  It can display system summary information as  well  as  a
   list  of processes or threads currently being managed by the Linux
   kernel.  The types of system summary  information  shown  and  the
   types,  order  and size of information displayed for processes are
   all user configurable and that configuration can be  made  persis‐
   tent across restarts.

via man top

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .