Killing the Processes with SIGKILL under UNIX


In UNIX and UNIX-like Operating Systems such as Linux, Mac OS X, When a program runs abruptly or if we were in a situation to terminate it then we need to pass the required signals to kill/terminate it. Learn the usage of SIGKILL -"kill" command.

If a program is long running and needs to be interrupted, then we press the interrupt key. This sends the signal SIGINT signal (numbered 2). By default it should kill the process, but this may be ignored by the process sometimes if some user-defined code is written to handle the signal.

Irrespective of what you do, there is a signal which will terminate the process immediately which is SIGKILL (9).
a. kill: Premature termination of a Process
• kill is an internal command in most of the shells, the external command /bin/kill is executed when the shell lacks the kill capability.
e.g: $ kill 105 -> which is just similar as kill –s TERM 105
multiple processes can be killed as, $ kill 105 128 4563 2135
• If all the processes to be killed have the same parent then the parent can be killed in order to kill all the children.
• If we use nohup to run a set of commands and log out, we can't kill the parent because the parentage is taken by init here, which can't be killed. Thus we need to kill one by one with respective PID.
• One can kill only the process owned by him, also system processes can't be killed.

b. $ sort –o emp.lst emp.lst & -> runs with PID 345 can be killed as -> $ kill 345 (or) kill $!
The variable '$!' stores the PID of the last background job.

c. Using kill with other signals:
By default the kill uses the signal SIGTERM (15) to terminate the process, but this can be ignored by some process and continue execution. This can be killed by the signal SIGKILL (9).

$ kill –s KILL 121 (recommended way) (or) $ kill -9 121 (same as before but not recommended)

Simple kill command (with TERM) won't kill the login shell, so this should be killed using KILL.
$ kill -9 $$ -> will kill the current shell where '$$' is a special variable which stores its PID.
$ kill –s KILL 0 -> kills all processes including the login shell
$ kill –l -> to list all the signal names and numbers available in the machine (or) can view the file /usr/include/sys/signal.h

To send a specific signal, use the -sign option, either with a signal number or a signal name (minus the "SIG" part), as shown in these examples:
• $ kill 123
• $ kill -1 123
• $ kill -HUP 123
• $ kill -f 123



Related Articles

Compare Windows NT server with Unix operating system

Windows NT is one of the oldest popular Operating System famous for it's networking features back in it's prime time. UNIX was and still is one of the stable Operating System that attracts lots of usage worldwide. Read the comparison to know how each of these OS fare with.

User ID and User types in UNIX Operating Systems

This article explains about, what exactly is an USER ID in UNIX operating system and how is that been represented and stored in "passwd" file with the user details. Also, read this article to know about the range of user ids and their special permissions.

More articles: Unix Operating System

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: