SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Thursday, December 13, 2007

Killing a process in Linux/Unix

How do I kill process in Linux?



Linux and all other UNIX like oses comes with kill command. The command kill sends the specified signal (such as kill process) to the specified process or process group. If no signal is specified, the TERM signal is sent.


Before I used to kill process in linux by finding the process Id.
For Ex: if i want to kill the process apache then i have to search the process first
(i.e) ps -ef|grep apache
Then i will display in the terminal like this


root 7302 1 0 15:00 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
nobody 7303 7302 0 15:00 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
nobody 7304 7302 0 15:00 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
nobody 7306 7302 0 15:00 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
nobody 7307 7302 0 15:00 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
nobody 7308 7302 0 15:00 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
nobody 7337 7302 0 15:01 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
root 9219 5954 0 15:34 pts/0 00:00:00 grep apache


Then we have to use kill -9 pid every time.I thing its waste of time
For that better we can group all the PID and we can go for kill
So i have started using the command
ps -ef| awk /apache/'{print $2}'
Now it will group all the processID like this


7302
7303
7304
7306
7307
7308
7337
9314


Now we can use kill command finally by passing the output of the above command
kill -9 `ps -ef| awk /apache/'{print $2}'`
I will display in the terminal like this "bash: kill: (9382) - No such process"
(since command itself searching for apache so that PID only 9382 so its displaying no such process)


Alternative Way I


Another way to kill the process in a single shot is by using the command pidof


pidof command is usefull to find the process ID of a running program.


Now you want to kill apache then type
pidof apache
7302 7303 7304 7306 7307 7308 7337 9219
Now we can use kill -9 `pidof apache` to kill the process

Alternative Way II


killall -9 apache

No comments :

Post a Comment