Doug Fir
Doug Fir

Reputation: 21282

Using grep with ps -ax to kill numerous applications at once (Mac)

I'm very new to shell scripting. I recently covered a tutorial of using bash with grep.

About 5 minutes ago there was, as it happened, a practical application for this function as my browser appeared to have crashed. I wanted to, for practice, use the shell to find the process and then kill it.

I typed ps -ax | grep Chrome

But many lines appeared with lots of numbers on the left (are these "job numbers" or "process numbers"?)

Is there a way to Kill all "jobs" where Chrome exists in the name of the process? Rather than kill each one individually?

Upvotes: 0

Views: 7286

Answers (3)

Johnny Maclane
Johnny Maclane

Reputation: 21

I like my solution better. Because adds a kind of wildcard to it. I did this:

mkdir ~/.bin

echo "ps -ef | grep -i $1 | grep -v grep | awk '{print $2}' | xargs kill -9" > ~/.bin/k

echo "alias k='/Users/YOUR_USER_HERE/.bin/k $1'" >> ~/.profile

chmod +x ~/.bin/k

Now to kill stuff you type:

k Chrome

or

k chrome

or

k chr

Just be careful with system processes or not to be killed processes.

Like if you have Calculator running and another one called Tabulator also running. If you type
k ulator it will kill'em both.

Also remember that this will kill ALL instances of the program(s) that match the variable after the command k.

Because of this, you can create two aliases inside your profile file and make 2 files inside .bin one with the command grep -i and the other without it. The one without the -i flag will be case and name sensitive.

Upvotes: 2

cabad
cabad

Reputation: 4575

You can use killall:

killall Chrome

Edit: Removed "-9" since it should be used as a last resource (was: killall -9 Chrome).

Upvotes: 2

ramonovski
ramonovski

Reputation: 414

I do not recommend using killall. And you should NOT use -9 at first.

I recommend a simple pkill:

pkill Chrome

Upvotes: 4

Related Questions