Thursday 24 October 2013

How to terminate linux process

    In this post I will mention some of the ways which can be used to kill a process in Linux environment. Most of the command line methods discussed here are also applicable to embedded linux targets.



Killing Process using System Monitor


   All the desktop based linux systems are equipped with System Monitor, it is linux version of Task Manager. In Processes tab, user can select a process and kill it. System Monitor for Ubuntu 12.04 and Fedora 18 are shown below.

Ubuntu 12.04 System Monitor
                       Ubuntu 12.04 System Montor


                          Fedora 18 System Monitor




Killing Process using xkill command


    Sometimes an unwanted process is opened and you want to close and don't bother looking in System Monitor to search for it. If that is the case then xkill is quite handy, you need to open up the terminal and execute xkill , it will change mouse pointer to a special cross symbol as a prompt for the user to select a window to be killed. Just click the window and you are done, you will not be prompted after you select the windows to be killed, so xkill should be used with care.

Killing Process using process name


    If you don't know the process id but know its name, you can kill it using either killall or pkill command. Suppose that I have gcalctool running on my system, by the way its a Calculator app on ubuntu machine. In order to kill it I will execute either of the two commands.
       pkill gcalctool
       killall gcalctool

   These commands can be used to kill processes names specified as regular expressions. Suppose I have two processes running on my system with names Process1 and Process2. They both can be killed in a single command using pkill and killall as follows.


       pkill Process
       killall -r Process*    
       
    As can be seen above killall is more recommended in this case. These commands should be used with care since they will terminate all instances of specified process name pattern.

Killing newest or oldest instance of process


    This is also a common scenario that user may want to kill the most recently created or least recently created instance of a process. pkill can be used to accomplish these. In order to kill newest instance of process following command can be used.

   pkill -n ProcessName    
          
    Similarly in order to kill the oldest instance of a process pkill will be invoked with -o switch as shown below.
   pkill -o ProcessName    
     Above options are quite safe as they will only terminate 1 process at most.


Killing a process using process id


    Last but not the least, a process can be killed by specifying the id using kill command. In order to get the id of process pgrep can be used as shown below
     pgrep NamePattern
    
    11370
    11379   
    
As can be seen above pgrep may return multiple process ids depending upon process name match.
     
Now in order to kill a process having id 11379 kill command can be invoked as shown below

   kill 11379
         
    By default kill sends signal SIGTERM(15) to the process which means the process should try to exit cleanly, however if you want to kill the process use SIGKILL(9) as shown below.
  kill -9 11379      
      





No comments:

Post a Comment