0

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Doug Fir
  • 19,971
  • 47
  • 169
  • 299

3 Answers3

4

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

I recommend a simple pkill:

pkill Chrome
ramonovski
  • 404
  • 3
  • 16
  • I tried that and was told "command not found" then the following kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] I'm using a Mac if that makes a difference (I'm very new to the differences of Unix and LInux and don;t even know if that's relevant. Thanks all the same – Doug Fir Jun 06 '13 at 21:31
  • Then install the `proctools` package (http://sourceforge.net/projects/proctools/) or try with the other solutions. And yes, if you are using MacOSX there is some differences with the `coreutils` and all the built-in commands. The next time try to specify it in the tags. – ramonovski Jun 06 '13 at 21:38
2

You can use killall:

killall Chrome

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

cabad
  • 4,555
  • 1
  • 20
  • 33
  • 3
    Do not use `kill -9`. The default of sending a TERM signal should be sufficient. – chepner Jun 06 '13 at 22:04
  • 1
    @chepner: +1 for this... I see all too often the '-9' : it should ONLY be used when the regular kill doesn't work, as it doesn't let the app "clean after it" properly (nor close file descriptors, etc) – Olivier Dulac Jun 06 '13 at 22:08
2

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.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75