MarksCode
MarksCode

Reputation: 8584

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?

Upvotes: 1

Views: 5497

Answers (3)

metallic
metallic

Reputation: 371

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.

Upvotes: 4

Blob
Blob

Reputation: 146

I use pgrep -- if you're sure you want to kill all the processes that match:

$ kill -9 `pgrep my-server`

Upvotes: 3

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382132

A simple solution is to use pkill:

sudo pkill my-server

Upvotes: 0

Related Questions