Pages

Tuesday, February 22, 2011

kill a non responsive process

So what do you do when you a process running on your Linux box hangs? Obviously you terminate it and start it again. But how do you terminate it is the question - series of commands or few clicks in System Monitor?
Frankly this post is for those who use the terminal - the "geeks".
Until today I used to kill programs "in this case" like 
$ ps -e | grep firefox
which gave me output like
2472 ?        00:00:00 firefox
2480 ?        07:16:45 firefox-bin  
And I used to fire
$ kill  2472 2480
to terminate the programs.
But today it just occurred to me to automate the process of looking up the process number and killing them. So I set out to use gawk and xargs.
Using gawk I got the process numbers extracted from the output of grep like this:-
$ ps -e | grep firefox | gawk '{print $1}'
2472
2480
and then pipelined this output to xargs kill to invoke kill on each pid.
$ ps -e | grep firefox | gawk '{print $1}' |xargs kill
It kills two processes but for completely restarting the application go to a terminal and type
$ ps -e | grep firefox | gawk '{print $1}' |xargs kill; firefox &
$ firefox & # starts firefox in the back ground
This served my purpose. Terminating two processes manually is not a big task, but why not automate it and make the computer do it for you if its possible?
And don't forget to have a look at man pages of gawk xargs and grep because that is the ultimate source of knowledge :)

No comments:

Post a Comment