-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd-cleanup
executable file
·52 lines (41 loc) · 1.39 KB
/
systemd-cleanup
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
#!/bin/bash
revert=0
user_units=()
if [[ "$1" == "--revert" ]]; then
revert=1
fi
set -u
while read -r unitpath; do
IFS='/' read -ra unitarray <<< "$unitpath"
# If the path leads to a drop-in file, a dir or a symlink, we skip it
# Example of a valid entry: usr/lib/systemd/system/[email protected]
if (( ${#unitarray[@]} > 5 )) || [[ ! -f $unitpath || -L $unitpath ]]; then
continue
fi
type=${unitarray[3]}
unit=${unitarray[4]}
case $type in
system)
if (( revert )); then
/usr/bin/systemctl revert "$unit"
fi
# Manager will be reloaded by post hook, so no explicit reload here
/usr/bin/systemctl disable --now --no-reload --no-warn "$unit"
;;
user)
if (( revert )); then
/usr/bin/systemctl --global revert "$unit"
fi
/usr/bin/systemctl --global disable --no-warn "$unit"
user_units+=("'${unit/@/@*}'")
;;
esac
done
if (( ${#user_units[@]} > 0 )); then
printf "==> %s user unit(s) being removed\n" "${#user_units[@]}"
printf " -> To stop them in current user manager, run \"systemctl --user stop %s\"\n" "${user_units[*]}"
if (( revert )); then
printf " -> To revert changes to them, run \"systemctl --user revert %s\"\n" "${user_units[*]}"
fi
fi
# vim: set ft=sh ts=4 sw=4 et: