[Bash] Kill all processes matching the given pattern
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:
ps -aux
: List all processes。grep "java"
: Filter all processes containingjava
in the commands.grep -v "grep"
: Surpress the current shell scipt process.awk '{print $2}'
: Preserve process id in column 2 and delete all other columns.- Loop through all pids and kill them.
Comments