[Bash] Kill all processes matching the given pattern

Linux & MacSystem Command

Recently I am in need of shutting down multiple java processes all at once in the development environment, so I finally came up with a bash script to obtain all java process ids and kill them.

ps -aux | grep "java" | grep -v "grep" | awk '{print $2}' | while read -r pid ; do
    echo "Shutting down PID: $pid"
    kill $pid
done

Details:

  1. ps -aux: List all processes。
  2. grep "java": Filter all processes containing java in the commands.
  3. grep -v "grep": Surpress the current shell scipt process.
  4. awk '{print $2}': Preserve process id in column 2 and delete all other columns.
  5. Loop through all pids and kill them.

8 Comments

  1. Hi! Someone in my Myspace group shared this website with us so I came to look it over.
    I’m definitely loving the information. I’m bookmarking and will be
    tweeting this to my followers! Fantastic blog and
    superb style and design.

  2. Good day! I know this is somewhat off topic but I was wondering if you knew where I could find a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  3. Excellent beat ! I wish to apprentice while you amend your web site, how can i subscribe
    for a blog site? The account helped me a acceptable deal.

    I had been a little bit acquainted of this your broadcast offered bright
    clear idea

  4. Good post however , I was wanting to know if you could write a
    litte more on this topic? I’d be very grateful if you could elaborate a little
    bit further. Thank you!

  5. I like what you guys tend to be up too. This sort of clever work and coverage!

    Keep up the amazing works guys I’ve incorporated you guys
    to my personal blogroll.

  6. An interesting discussion is worth comment. I do
    believe that you need to publish more about this issue, it may not be a taboo
    matter but typically folks don’t talk about such issues.
    To the next! Cheers!!

Leave a Reply

Your email address will not be published. Required fields are marked *