Monday, May 25, 2009

Killing processes started in the current terminal

Imagine you're running some application, lets call it "foo", using an unix user shared with your work colleagues, and this application is also launched by them at the same time you're working. So along the time you may have several instances of this application, several processes with distinct PIDs. And all the sudden you need to kill all the instances that you previously launched but you need to avoid to kill the ones being used by your fellow colleagues. This is one way to do the trick.

Start by retrieving your tty (teletypewriter),

bash$ tty
/dev/pts/4

And then kill the process "foo" for the current "tty" you're using,

bash$ kill `ps -le | grep foo | grep "pts/4" | cut -d" " -f6 `


PS: If know a way to include the retrieval of the tty inside the grep command let know =)

2 comments:

  1. No need to grep for the terminal. If you invoke ps without -e it will give only the processes attached to your terminal. But even if you want to choose the terminal you can do ps -t
    kill `ps -t pts/4 -o comm,pid | grep foo | tr -s ' ' | cut -d" " -f 2`

    David ;-)

    ReplyDelete
  2. I wasn't able to run that solution in GNU/Linux, but aparently it works on Solaris and OS X.
    Thanks mate :)

    ReplyDelete