Wednesday, October 20, 2010

Issue Single Command To Kill a Process In Linux/Unix system

I am so tired of issuing back and forth this 'ps axu' command thing. I was debugging stuff in JBoss and I do always 'ps axu|grep jboss' then I took the process id and kill it by:

kill -9 processid

Then I just tried killing two process id at a time like for example:

kill -9 processid1 processid2

and that will actually kill two process id, however, that's still tedious but it does help in some way.

So I figure it out that I can use awk tool for this and get the column where the processid would be printed. So I found out a way with just one command to kill a process either in Linux/Unix system. I did this in my Mac OS X Snow Leopard.

kill -9 $(ps -e|grep jboss|awk '{ print $1 }') // works in Snow Leopard


kill -9 $(ps -re|grep sl1 | awk -F ':' '{ print $3}'| awk '{print $1}') // Works in Mac OS X 10.8 Snow Lion


kill -9 $(ps axu|grep jboss|awk '{ print $2 }') // Linux 

the '$1' there is the index number where the process id is located. If  you have some alias with your grep, don't forget to unalias it just to assure, because I do have a alias for my grep having -rn on it.

Hope this helps, with just one command, it would make your life easy. You can make this as an alias if you want also.

Additionally, you can also do,

kill -9 $(ps -re|grep -rE sl1 | awk -F ':' '{ print $3}'| awk '{print $1}')

No comments: