Sunday 29 January 2012

kill


kill  
kill is used to send a signal to the process, though the name suggests that it is used only to terminate a process. But, kill  helps to terminate a process more often than not. When some process  is  run unexpectedly or accidentally, it  might have to be aborted ,for which kill helps just like CTRL+ALT+DEL in windows .

Example 1)
ps command gives you the process id s  and other details of various commands, if you want to stop any command, just check the corresponding PID of the  process(the command) which you want to terminate ,then use kill  to stop that process.

Consider the output of ps,

/home/b6789$ps  -fu  b6789
UID  PID  PPID C STIME   TTY TIME  CMD
b6789   352266      1     0 05:32:38     -  0:14 java  /usr/bin/wc  –l   
b6789   368734      1     0 05:32:42     -  0:07 [wc]
------b6789   372912     120 0 05:32:43      -  5:10 java  /usr/bin/java/dev2.java            
b6789   409806      1     0 05:32:39     -  0:13 java  /usr/bin/java/start-atm.java

                                                                                          this process is consuming cpu and has to be terminated,

/home/b6789$kill  372912
This sends the signal SIGTERM to the process 372912, and terminates it.
Sometimes the process may refuse to terminate just by SIGTERM, in that case  use SIGKILL signal(kill -9)
/home/b6789$kill    -9   372912

This signal cannot be ignored . You can kill any of the process initiated by you, i:e from your  user id this way .A root user has the privilege of killing any user’s  processes .

Example 2)
You  can also  get  the specific PID  for your command without having to search it using grep and awk along with ps. Then kill the PID which you have retrieved.

If you had to kill the CMD start-atm.java above,
/home/b6789$j_pid=`ps  –fu  $LOGNAME | grep  start-atm.java | grep –v grep | awk ’{print  $2}’`
/home/b6789$echo  $ j_pid
409806
/home/b6789$kill   -9   $ j_pid

You can also use xargs  to kill process of a particular type all  at once .

Example 3)
There is a method to kill all the processes initiated by you,            

/home/b6789$kill  -9   -1

This will kill all the processes of your user id and also  closes  your login session. It is a very dangerous command ,so think thrice  and  check whether any required process is still running using ps before you press enter.




You can send many kinds  of  signals to a process(not all)   by using  kill  -<signal>  PID. The various signal numbers and their names are listed below  (for AIX) in detail .A short list can also be viewed
by using  kill  -l (lowercase  alphabet  L).

Name      num       Description
SIGHUP      1           hangup, generated when terminal disconnects
SIGINT       2           interrupt, generated from terminal special char
SIGQUIT    3           (*) quit, generated from terminal special char
SIGILL        4           (*) illegal instruction (not reset when caught)
SIGTRAP    5           (*) trace trap (not reset when caught)
SIGABRT    6          (*) abort process
SIGEMT     7           EMT instruction
SIGFPE       8           (*) floating point exception
SIGKILL     9           kill (cannot be caught or ignored)
SIGBUS     10         (*) bus error (specification exception)
SIGSEGV   11         (*) segmentation violation
SIGSYS      12         (*) bad argument to system call
SIGPIPE    13         write on a pipe with no one to read it  
SIGALRM  14         alarm clock timeout
SIGTER M  15        software termination signal
SIGURG     16         (+) urgent condition on I/O channel
SIGSTOP   17         (@) stop (cannot be caught or ignored)
SIGTSTP   18         (@) interactive stop
SIGCONT                  19         (!) continue (cannot be caught or ignored)
 SIGCHLD   20        (+) sent to parent on child stop or exit                       
 SIGTTIN   21         (@) background read attempted from control terminal            
 SIGTTOU   22        (@) background write attempted to control terminal             
 SIGIO       23         (+) I/O possible, or completed                                 
 SIGXCPU  24         cpu time limit  exceeded
 SIGXFSZ   25         file size limit exceeded
 SIGMSG    27         input data is in the ring buffer                               
 SIGWINCH  28      (+) window size changed                                        
 SIGPWR    29        (+) power-fail restart                                         
 SIGUSR1   30        user defined signal 1                                           
 SIGUSR2   31        user defined signal 2                                          
 SIGPROF   32        profiling time alarm
 SIGDANGER 33     system crash imminent; free up some page space                 
 SIGVTALRM 34     virtual time alarm
 SIGMIGRATE 35   migrate process                                                
 SIGPRE    36         programming exception                                          
 SIGVIRT   37         AIX virtual time alarm                                         
 SIGALRM1  38      m:n condition variables - RESERVED - DON'T USE                 
 SIGTALRM  38      per-thread alarm clock                                         
 SIGWAITING 39   m:n scheduling - RESERVED - DON'T USE                          
 SIGRECONFIG 58  Reserved for Dynamic Reconfiguration Operations                
 SIGCPUFAIL 59    Predictive De-configuration of Processors – (RESERVED - DON'T USE   )                   
                                 
 SIGKAP    60              keep alive poll from native keyboard                            
 SIGGRANT  SIGKAP  monitor mode granted                                          
 SIGRETRACT 61      monitor mode should be relinquished                            
 SIGSOUND  62        sound control has completed                                     
 SIGSAK    63         secure attention key                                           


Example 4)
You can  stop a running process in the midway and then continue it from that point using kill command.
Assume  you are running a ftp command and want to stop  the file transfer.
/home/b6789$ ftp   10.42.67.125
……
…….
put abigfile.txt
#####################################

Now in between if you want to suspend it. take an other session for the same user and run
kill  -17 <ftp_PID>,

where <ftp_PID>  is the PID of the ftp command. This sends the signal SIGSTOP to the command. you can observe that the file transfer is stopped.

To restart the command,simply run
Kill -19  <ftp_PID> , which sends the signal SIGCONT which is to continue  a  process. The file transfer starts from the point where it stopped.

Similarly other commands ,jobs  or utilities can be stopped and continued using kill -17 and kill -19 resp.


Example 5)
To Kill all the child processes of a particular process use
kill  -9  -<parent pid>
to kill all child processes of PID 204567
kill   -9  -204567


No comments:

Post a Comment