1

Is it possible to kill all find process with one command?

I do not want to kill each process as kill -9 25295 , kill -9 11994 , etc.. Rather, what I want is a simple way or command that kill all find process (my target is to perfrom this action on linux and solaris machines).

$ ps -ef | grep find 
root 25295 25290   0 08:59:59 pts/1 0:01 find /etc -type f -exec grep -l 100.106.23.152 {} ; -print
root 11994 26144   0 09:04:18 pts/1 0:00 find /etc -type f -exec grep -l 100.106.23.153 {} ; -print
root 25366 25356   0 08:59:59 pts/1 0:01 find /etc -type f -exec grep -l 100.106.23.154 {} ; -print
root 26703 26658   0 09:00:05 pts/1 0:01 find /etc -type f -exec grep -l 100.106.23.155 {} ; -print
yael
  • 2,433
  • 5
  • 31
  • 43
  • 1
    Having answered some of your questions and looked at may others I think that rather than an Q&A site it's time to talk to your manager about getting some basic education in the tools that you are using. – user9517 Nov 25 '12 at 15:08
  • @lain this would be so *old school*! Neat! – kubanczyk Nov 25 '12 at 15:21
  • @lain what you think about the solution: fuser -k /tmp/test.sh ? , (for linux and solaris ) – yael Nov 25 '12 at 15:48

3 Answers3

5

This will work on both Linux and Solaris and do precisely what you need:

pgrep -f 'find /etc'     # verify the listing before proceeding
pkill -9 -f 'find /etc'

In your situation, avoid killall. If you use it on Linux, sooner or later you will mistake the ssh sessions, run it on Solaris, creating unnecessary risk.

The -f option of pgrep/pkill means to match the entire command line. In case you need to match path of the program or script (/var/tmp/test.sh), this works if you had run it with the entire path. To be precise, you only need to escape the . so you need

pkill -9 -f '/var/tmp/test\.sh'

If you have run the same program as ./test.sh you need to kill it as such. See -f option in ps.

kubanczyk
  • 13,812
  • 5
  • 41
  • 55
  • Someday one of my coworker typed pkill -v foobar , thinking -v would increase verbosity;obviously it was on a production database server... Be careful everyone when you use pkill ;) –  Nov 25 '12 at 15:24
  • Moreover, `f` is very close to `v` on the keyboard :) – kubanczyk Nov 25 '12 at 15:29
  • @kubanczyk what about fuser -k /tmp/test.sh - I think is the best solution for linux and solaris - what you think ? – yael Nov 25 '12 at 15:46
  • @yael Never used fuser -k, because it does not support regular expressions as patterns. So I don't have any experience to share. – kubanczyk Nov 25 '12 at 16:34
  • not sure I try to kill the programs /tmp/test.pl ( was 12 process running on both time ) , and fuser -k killed all (test.pl) process , , – yael Nov 26 '12 at 07:29
3

Use pkill find which is a variant of pgrep (process grep). On Linux, killall find would also work.

Sven
  • 98,649
  • 14
  • 180
  • 226
0

Yes, you can use the killall command

killall find 
Dennis Kaarsemaker
  • 19,277
  • 2
  • 44
  • 70
  • 3
    But be _very_ careful if going between Linux and Solaris, because at least on older versions of SunOS that I used, `killall` literally _kills all processes_, without the process matching and selection of the Linux version. – mattdm Nov 25 '12 at 14:42
  • 1
    As far as I know, this is still the case. – Sven Nov 25 '12 at 14:47
  • 1
    As a side note, the behavior is not nearly as funny on Solaris as on AIX. There, `killall` *doesn't* kill the current shell, so poor user has no idea how much fun just happened in the background :) – kubanczyk Nov 25 '12 at 15:24