-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstop-workspace-apps.sh
More file actions
executable file
·98 lines (81 loc) · 2.21 KB
/
Copy pathstop-workspace-apps.sh
File metadata and controls
executable file
·98 lines (81 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
set -euo pipefail
WORKSPACE_ROOT="$(cd "$(dirname "$0")" && pwd)"
KNOWN_MAIN_CLASSES=(
"EurekaServerApplication"
"GatewayServerApplication"
"InventoryMsApplication"
"UserMsApplication"
)
echo "Stopping Java apps running from: $WORKSPACE_ROOT"
# Find candidate Java processes and match by:
# 1) command line contains workspace path, OR
# 2) command line contains one of known app main classes, OR
# 3) process current working directory is under workspace root.
PIDS=""
while IFS=$'\t' read -r pid cmdline; do
[[ -z "$pid" || -z "$cmdline" ]] && continue
is_match=false
if [[ "$cmdline" == *"$WORKSPACE_ROOT"* ]]; then
is_match=true
else
for class_name in "${KNOWN_MAIN_CLASSES[@]}"; do
if [[ "$cmdline" == *"$class_name"* ]]; then
is_match=true
break
fi
done
fi
if [[ "$is_match" == false ]]; then
cwd="$(lsof -a -d cwd -p "$pid" -Fn 2>/dev/null | awk '/^n/ { sub(/^n/, "", $0); print; exit }')"
if [[ -n "$cwd" && "$cwd" == "$WORKSPACE_ROOT"* ]]; then
is_match=true
fi
fi
if [[ "$is_match" == true ]]; then
PIDS+="$pid"$'\n'
fi
done < <(
ps -axo pid=,command= | awk '
{
pid = $1
sub(/^[0-9]+[[:space:]]+/, "", $0)
if ($0 ~ /(^|[[:space:]])([^[:space:]]*\/)?java([[:space:]]|$)/) {
print pid "\t" $0
}
}
'
)
PIDS="$(printf "%s" "$PIDS" | awk 'NF' | sort -u)"
if [[ -z "$PIDS" ]]; then
echo "No matching workspace Java processes found."
exit 0
fi
echo "Found process IDs:"
printf '%s\n' "$PIDS"
echo "Sending SIGTERM..."
while IFS= read -r pid; do
[[ -z "$pid" ]] && continue
kill -TERM "$pid" 2>/dev/null || true
done <<< "$PIDS"
# Wait up to 10 seconds for graceful shutdown.
for _ in {1..10}; do
sleep 1
remaining=""
while IFS= read -r pid; do
[[ -z "$pid" ]] && continue
if kill -0 "$pid" 2>/dev/null; then
remaining+="$pid"$'\n'
fi
done <<< "$PIDS"
if [[ -z "$remaining" ]]; then
echo "All workspace applications stopped gracefully."
exit 0
fi
done
echo "Some processes are still running. Sending SIGKILL..."
while IFS= read -r pid; do
[[ -z "$pid" ]] && continue
kill -KILL "$pid" 2>/dev/null || true
done <<< "$remaining"
echo "Done."