Aliases to Kill Linux Processes

Writing by on Wednesday, 20 of April , 2011 at 9:30 pm

As a developer I’m constantly restarting processes. For instance, if I have 2 Java Process (Server/Web) and an Eclipse Java Process which makes 3 Java Processes. The problem is to kill only the server and web processes and leave the eclipse java process intact.

Solution 1 (The Hard and Manual approach):

Find the java processes and kill them manually

ps -ef | grep java
kill -9 1
kill -9 2

Solution 2 (The Simplified Approach – killjava):

Create an alias or run the following:

ps -A|grep java|sed -e “s/\s*\([0-9]*\).*/\1/”|xargs kill -9

This will kill all java processes – which means you will need to restart eclipse each time you run this.

Solution 3 (The Tuned Approach – killweb):

Create an alias or run the following based on a keyword (say, Web process remote_debug port 4096):
ps -A|grep java|sed -e “s/\s*\([0-9]*\).*/\1/” | xargs ps -p | grep 4096 | sed -e “s/\s*\([0-9]*\).*/\1/” | xargs kill -9

Create an alias or run the following based on a keyword (say, Server process remote_debug port 2048):
ps -A|grep java|sed -e “s/\s*\([0-9]*\).*/\1/” | xargs ps -p | grep 2048 | sed -e “s/\s*\([0-9]*\).*/\1/” | xargs kill -9

This will kill only the specific processes you want to terminate.

Solution 4 (The BEST Way – Combine 1 and 2 in a single alias – killdebug):

Create an alias or run the following (Use egrep to look for multiple keywords above):

ps -A|grep java|sed -e “s/\s*\([0-9]*\).*/\1/” | xargs ps -p | egrep “2048|4096” | sed -e “s/\s*\([0-9]*\).*/\1/” | xargs kill -9

This will kill all processes you WISH to terminate.

I have aliases for Solutions 2, 3 and 4 depending on what I’m trying to do. So there you go – hope that helps!

Leave a comment

Category: Linux,Tips and Tricks

No Comments

No comments yet.

Leave a comment

You must be logged in to post a comment.

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.