ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Script to kill multiple app spawns

    IT Discussion
    script bash linux
    2
    4
    897
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stacksofplatesS
      stacksofplates
      last edited by stacksofplates

      So some of our applications spawn multiple processes with the same name. I've also manually started the same thing multiple times accidentally. I'm too lazy to either kill each pid separately or do ps aux | grep -i <name> | awk '{ print $2 }' in a for loop. So I made this script. Just pass the grep string that you want to kill or a -i for an interactive session.

      #!/bin/bash
      
      #Script to kill multiple spawns of an application
      
      #Script functions
      function script_help () {
        echo "
            Usage: $(basename $0) [options] app-name 
      
                -i   Ineractive mode
      
                -h   this help text
      
                app-name       Name of application to kill
      
            Example:
              $(basename $0) firefox"
      
        exit ${1:-0}
      }
      
      function interactive_kill () {
      
        echo "Search string"
        read searchString
      
        for pid in $(ps aux | grep -i $searchString | awk '{ print $2 }')
        do
          kill -9 $pid
        done
      
        exit ${1:-0}
      
      }
      
      function argument_kill () {
      
        for pid in $(ps aux | grep -i $searchString | awk '{ print $2 }')
        do
          kill -9 $pid
        done
      
        exit ${1:-0}
      
      
      
      }
      
      #Show help if no arguments or options are passed
      [[ ! "$*" ]] && script_help 1
      OPTIND=1
      
      
      #Read command line options
      while getopts "ih" opt; do
          case "$opt" in
            i) interactive_kill ;;
            h) script_help ;;
            \?) script_help 1 ;;
          esac
      done
      shift $(($OPTIND-1));
      
      #Run argument function
      searchString=$1
      argument_kill
      
      1 Reply Last reply Reply Quote 1
      • scottalanmillerS
        scottalanmiller
        last edited by

        Why not just killall?

        stacksofplatesS 1 Reply Last reply Reply Quote 1
        • stacksofplatesS
          stacksofplates @scottalanmiller
          last edited by

          @scottalanmiller said in Script to kill multiple app spawns:

          Why not just killall?

          Mostly just for our use. When we run the distributed solves it opens a process per however many cores we designate for that system, plus a couple other management processes. All of the solver processes have the same name, but the management processes are slightly different. So I'd still have to do a grep for the pid of each.

          Plus I'm just doing some practicing.

          1 Reply Last reply Reply Quote 0
          • stacksofplatesS
            stacksofplates
            last edited by stacksofplates

            I guess I could do a wildcard at the beginning and end of whatever I'm searching for with killall but that freaks me out a little. I could accidentally do a lot of damage with that.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post