Search code examples
linuxmacossshcommand-lineps

Kill 'ps aux' output in one line


I have to do the same thing many times a day:

ps aux

look for process running ssh to one of my servers ...

kill -9 <pid>

I'm looking to see if I can alias process into one line. The output of ps aux is usually something like this:

user   6871   0.0  0.0  4351260      8   ??  Ss    3:28PM   0:05.95 ssh -Nf -L 18881:my-server:18881
user   3018   0.0  0.0  4334292     52   ??  S    12:08PM   0:00.15 /usr/bin/ssh-agent -l
user   9687   0.0  0.0  4294392    928 s002  S+   10:48AM   0:00.00 grep ssh

I always want to kill the process with the my-server in it.

Does anyone know how I could accomplish this?


Solution

  • for pid in $(ps aux | grep "[m]y-server" | awk '{print $2}'); do kill -9 $pid; done

    ps aux | grep "[m]y-server" | awk '{print $2}' - this part gives you list of pids processes that include "my-server". And this script will go through this list and kill all this processes.