-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.sh
executable file
·58 lines (50 loc) · 1.77 KB
/
cleanup.sh
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
#!/bin/bash
# Set the registry directory, defaulting to /var/run/passenger-instreg if not set
REGISTRY_DIR="${REGISTRY_DIR:-/var/run/passenger-instreg}"
# Check if the directory exists
if [[ ! -d "$REGISTRY_DIR" ]]; then
echo "Registry directory $REGISTRY_DIR does not exist."
exit 1
fi
# Get a list of folders sorted by modification time (newest last)
folders=($(ls -td "$REGISTRY_DIR"/*/))
# If there is more than one folder, we skip the latest one
if [[ ${#folders[@]} -gt 1 ]]; then
echo "Alive PID is ${folders[0]}"
# Loop through all folders except the last (newest)
for folder in "${folders[@]:1}"; do
if [[ ! -f "$folder/watchdog.pid" ]]; then
echo "No watchdog.pid found in $folder"
continue
fi
if [[ ! -f "$folder/web_server_info/child_processes.pid" ]]; then
echo "No child_processes.pid found in $folder"
continue
fi
nids=($(grep -v '^$' "$folder/web_server_info/child_processes.pid"))
pid=$(cat "$folder/watchdog.pid")
# Check if any of the PIDs exist
for nid in "${nids[@]}"; do
if [[ -n "$nid" ]] && kill -0 "$nid" 2>/dev/null; then
# If any PID exists, skip the folder
echo "Worker $pid still running so skip $folder"
continue 2 # Skip further processing for this folder
fi
done
if ! kill -0 "$pid" 2>/dev/null; then
# Check if the process still exists
echo "Process $pid not found, deleting folder $folder"
rm -rf "$folder"
else
kill $pid
tail --pid=$pid -f /dev/null
echo "Killed PID $pid in $folder"
rm -rf "$folder"
fi
done
elif [[ ${#folders[@]} -gt 0 ]]; then
echo "No folders to kill processes in, only one PID found which ${folders[0]}"
else
echo "Something wrong, trying to restart."
systemctl restart nginx
fi