Terminator – the power of grep and awk

I like to call this little combination of command the terminator

$ kill `ps -ef | grep vlc | grep -v grep | awk ‘{print $2}’`

Terminator kills the process(es) matching the name provided as the input to the first grep.

I read this little trick somewhere I cant remember all credit goes to the author.

How this works:

  • ps -ef lists all the process running along with its pid
  • grep lists only those matching its parameters
  • grep -v grep removes the grep process itself from showing up in ps -ef
  • awk extracts the pid and voila kill terms them off.

To make this little guy a command

$ cat > terminator

#read processname
kill `ps -ef|grep $1|grep -v grep|awk ‘{print $2}’`

$ chmod +x terminator

# mv terminator /bin

$ terminator vlc

works the same as typing the command in full.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: