#!/usr/bin/env bash # Stop every process in the module. # # Two rules, both learned the hard way while building this repository: # # 1. Match the MAIN CLASS, never 'spring-boot' or 'java'. `pkill -f spring-boot` also matches # the shell command line that launched the application, so it kills your own shell. # # 2. Restrict the match to processes that are actually a JVM, and exclude this script and its # parent. A bracketed pattern like '[A]uthServerApplication' stops the pattern matching the # grep itself - but it does NOT stop it matching an ancestor shell whose command line # happens to contain that string, which is exactly what happens when you paste a here-doc # containing the class name into a terminal and then run this script from it. The victim # process dies with exit 137 and no output, which is a memorable afternoon. set -eu SELF=$$ PARENT=$PPID ps -eo pid,ppid,comm,args | awk -v self="$SELF" -v parent="$PARENT" ' $1 != self && $1 != parent && $3 ~ /^java/ && $0 ~ /com\.ankurm\.s2s\./ { print $1 } ' | while read -r pid; do kill -9 "$pid" 2>/dev/null || true done sleep 1