Script to kill multiple app spawns
-
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
-
Why not just killall?
-
@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.
-
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.