linux/solaris kill many proccess with one command

5,966

Solution 1

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.

Solution 2

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

Share:
5,966

Related videos on Youtube

yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    yael over 1 year

    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
    
    • user9517
      user9517 over 11 years
      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.
    • Naveed Abbas
      Naveed Abbas over 11 years
      @lain this would be so old school! Neat!
    • yael
      yael over 11 years
      @lain what you think about the solution: fuser -k /tmp/test.sh ? , (for linux and solaris )
  • mattdm
    mattdm over 11 years
    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.
  • FooBee
    FooBee over 11 years
    As far as I know, this is still the case.
  • yael
    yael over 11 years
    is it possible to kill proccess as script for example - pkill /var/tmp/test.sh ?
  • Admin
    Admin over 11 years
    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 ;)
  • Naveed Abbas
    Naveed Abbas over 11 years
    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 :)
  • Naveed Abbas
    Naveed Abbas over 11 years
    Moreover, f is very close to v on the keyboard :)
  • yael
    yael over 11 years
    @kubanczyk what about fuser -k /tmp/test.sh - I think is the best solution for linux and solaris - what you think ?
  • Naveed Abbas
    Naveed Abbas over 11 years
    @yael Never used fuser -k, because it does not support regular expressions as patterns. So I don't have any experience to share.
  • yael
    yael over 11 years
    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 , ,