-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclose-userspace.sh
executable file
·94 lines (75 loc) · 2.66 KB
/
close-userspace.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
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
#!/usr/bin/env bash
# Function to get the list of running GUI applications and their window IDs
running_gui_apps() {
declare -A app_windows
# Loop through all open windows (ids)
for win_id in $(wmctrl -l | cut -d' ' -f1); do
# Test if window is a normal window
if xprop -id "$win_id" _NET_WM_WINDOW_TYPE | grep -q '_NET_WM_WINDOW_TYPE_NORMAL'; then
# Get the process ID of the window
pid=$(xprop -id "$win_id" _NET_WM_PID | cut -d' ' -f3)
# Get the process name from the process ID using ps
pname=$(ps -p "$pid" -o comm=)
# Add to the associative array with process name as key and window ID as value
app_windows["$win_id"]="$pname"
fi
done
echo "${!app_windows[@]}"
}
# Function to close all GUI applications gracefully using wmctrl
close_all_applications() {
local window_ids=("$@")
local retries=(0.1 0.5 1)
for retry in "${retries[@]}"; do
for win_id in "${window_ids[@]}"; do
# Send the close signal to the window
wmctrl -ic "$win_id"
done
sleep "$retry"
# Check if all windows are closed
window_ids=$(running_gui_apps)
if [ -z "$window_ids" ]; then
echo "Closed all running GUI applications gracefully."
return
fi
done
# If not all closed, escalate to SIGQUIT
escalate_signal "SIGQUIT" "${window_ids[@]}"
}
# Function to escalate signals
escalate_signal() {
local signal="$1"
local window_ids=("$@")
local retries=(0.1 0.2 0.3)
for retry in "${retries[@]}"; do
for win_id in "${window_ids[@]}"; do
# Get the process ID of the window
pid=$(xprop -id "$win_id" _NET_WM_PID | cut -d' ' -f3)
# Send the specified signal to the process
kill -s "$signal" "$pid"
done
sleep "$retry"
# Check if all windows are closed
window_ids=$(running_gui_apps)
if [ -z "$window_ids" ]; then
echo "Closed all running GUI applications with $signal."
return
fi
done
# If not all closed, escalate to the next signal
case "$signal" in
"SIGQUIT")
escalate_signal "SIGTERM" "${window_ids[@]}"
;;
"SIGTERM")
escalate_signal "SIGKILL" "${window_ids[@]}"
;;
"SIGKILL")
echo "Forcefully closed all remaining GUI applications with SIGKILL."
;;
esac
}
# Call the function to get the list of window IDs of running GUI applications
window_ids=$(running_gui_apps)
# Close all running GUI applications
close_all_applications $window_ids