Linux Process Management: Command Line - gHacks Tech News

Linux Process Management: Command Line

Previously I wrote about using GUI tools for Linux process management (Get To Know Linux: Process Management.) Both the GNOME and the KDE tools are outstanding, user-friendly solutions to an issue that can often become troublesome. But what if you are running a headless server? Or what happens if that runaway process is X Windows itself? What do you do? You open up a terminal window and issue a command. In this article you are going to learn just that, killing rogue processes by command line.

Although you might think this a system of process management that should be avoided by the new user, you should consider that there may be times when knowing this process can really save your skin. So, although you may tend to shy away from the command line tools, this would be one of those that is good to know in a pinch.

The Commands

The first thing you have to understand is the commands you will be using. There are two commands that are useful: kill and killall. The kill command is used in conjunction with a process ID (PID) and the killall command is used in conjunction with command names. Your first reaction is most likely "How do I get the PID of an application?" Well, that will take another command.

Learn By Example

Let's use Firefox as an example. Say, for some reason, Firefox has frozen and you can not get it to respond. Open up a terminal and issue the command killall firefox if that doesn't kill Firefox then Firefox was started with a different command. To find out what that command is you could employ the ps command like so:

ps aux|grep firefox

The output would look something like:
[jlwallen ~]$ ps aux|grep firefox
jlwallen 10199  3.8 28.8 417396 223764 ? RNsl Feb23  77:22 /usr/lib/mozilla/firefox-bin -a firefox
jlwallen 13434  0.0  0.0   3796   688 pts/0  RN+  20:14   0:00 grep firefox

You see the line that includes firefox-bin? That is most like what you need to kill. So issue the command killall firefox-bin and the frozen Firefox will go away.

If you look up at the command output again you can see the output includes the PID of each command. The PID associated with firefox-bin would be 10199. You could issue the command kill 10199 and Firefox would close as well.

Frozen X

What if X Windows freezes and you know the culprit was, say, Evolution? You can't get to a terminal window so what do you do? You have to go to a different virtual console. To do this you will hit the key combination Ctrl-Alt-F* (Where * is a number 1-9. This number will differ depending upon which virtual console X is running.) Once you are in a virtual consol you will have to log in as your user and then issue the kill command. You can then go back to the virtual console running X and, hopefully, it will be back under your control.

Final Thoughts

It's not as user-friendly as either the GNOME or KDE tools, but this method is a very valuable method of process control. Some day you will need to have this method in your tool belt and, when you do, you'll be glad you have it.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. abhijit said on February 25, 2009 at 5:59 am
      Reply

      In case a window stops responding, you can also press Alt+Ctrl+Esc and then click on the application window. This kills the application without having to go to another terminal

    2. hybrid-kernel said on February 25, 2009 at 6:24 am
      Reply

      Why not mention (h)top as well? Very useful tools they are.

      @abhijit: Is this a gnome only thing? I don’t have it in openbox. If anything, I just ‘alt+f2’ and ‘xkill’ and then click window. (Too lazy to bind a key-combo)

    3. jack said on February 25, 2009 at 2:59 pm
      Reply

      Ctl-Alt-Esc only works in KDE.

      and you are right, you can use XKill but it’s always been, in my opinion, safer to use the command. you accidentally click the xkill cursor on your root window and your root window could bonk out or X could restart.

    4. gB said on February 26, 2009 at 10:09 am
      Reply

      Don’t know if it’s universal, but I use ps -A to get a listing of all PID’s and their corresponding names. Just an alternative….

    5. nitesh said on August 19, 2009 at 2:25 pm
      Reply

      thanks fror informations

    6. Anonymous said on December 19, 2009 at 11:55 am
      Reply

      I realize that this was a while ago, but there are better ways. To avoid having grep appear in your example, use a semantically null modifier:
      [jlwallen ~]$ ps aux|grep firefox

      As for killall, it has a very different meaning on Solaris. This invited a better solution and, sure enough, one now exists. Instead of killall, use pkill. While pkill has some new options, that’s not why it should replace killall; pkill has a sibling, pgrep, and pgrep is awesome.

      Sometimes trying to kill a certain browser/platform made by Mozilla is like playing whack-a-mole. In Debian at least, that browser likes to hide processes under different names. So, when I need to kill it I run pgrep first to see what name it’s hiding under and make sure that there won’t be any collateral damage:
      [jlwallen ~]$ pgrep -l fox; pgrep -l icew; pgrep -l xul; pgrep -l moz

    Leave a Reply