forked from TapeWerm/MCscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisable_services.sh
executable file
·82 lines (72 loc) · 2.77 KB
/
disable_services.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
#!/usr/bin/env bash
# Exit if error
set -e
syntax='Usage: disable_services.sh'
# Current scripts
scripts=(mc_backup.sh mc_cmd.sh mc_color.sed mc_getjar.sh mc_stop.sh)
scripts+=(mcbe_autoupdate.sh mcbe_backup.sh mcbe_getzip.sh mcbe_log.sh mcbe_update.sh)
scripts+=(disable_services.sh enable_services.sh move_backups.sh move_servers.sh)
# Removed scripts
scripts+=(MCbackup.sh MCcolor.sed MCgetJAR.sh MCrunCmd.sh MCstop.sh)
scripts+=(MCBE_Bot.sh MCBEautoUpdate.sh MCBEbackup.sh MCBEgetZIP.sh MCBElog.sh MCBEupdate.sh)
scripts+=(DisableServices.sh EnableServices.sh MoveServers.sh)
# Current services
services=(mc@*.socket mc@*.service mc-backup@*.timer mc-rmbackup@*.service)
services+=(mcbe@*.socket mcbe@*.service mcbe-backup@*.timer mcbe-getzip.timer mcbe-autoupdate@*.service mcbe-rmbackup@*.service mcbe-log@*.service)
# Removed services
services+=(mcbe-autoupdate@*.timer mcbe-bot@*.service mcbe-bot@*.timer mcbe-log@*.timer)
# Current units
units+=([email protected] [email protected] [email protected] mcbe-getzip.service mcbe-getzip.timer [email protected] [email protected] [email protected] [email protected])
# Removed units
case $1 in
--help|-h)
echo "$syntax"
echo 'Find enabled services from MCscripts, prompt user to disable them and remove their files, and list services in ~mc/disabled_services.txt.'
exit
;;
esac
if [ "$#" -gt 0 ]; then
>&2 echo Too much arguments
>&2 echo "$syntax"
exit 1
fi
instances=$(systemctl show "${services[@]}" -p Id --value | grep .)
if [ -n "$instances" ]; then
while read -r instance; do
if [ "$(systemctl is-enabled "$instance" 2> /dev/null)" = enabled ]; then
enabled+=("$instance")
elif [ "$(systemctl is-active "$instance" 2> /dev/null)" = active ]; then
active+=("$instance")
fi
# Bash process substitution
done < <(echo "$instances")
fi
if [ -z "${enabled[*]}" ] && [ -z "${active[*]}" ]; then
echo No services enabled
exit
fi
echo "Enabled services: ${enabled[*]}"
echo "Active but not enabled services: ${active[*]}"
echo "Enter Y to disable services and remove their files (make sure people aren't playing first)"
read -r input
input=$(echo "$input" | tr '[:upper:]' '[:lower:]')
if [ "$input" != y ]; then
>&2 echo "$input != y"
exit 1
fi
echo "${enabled[*]}" | sudo tee ~mc/disabled_services.txt > /dev/null
if [ -n "${enabled[*]}" ]; then
sudo systemctl disable "${enabled[@]}" --now
fi
if [ -n "${active[*]}" ]; then
sudo systemctl stop "${active[@]}"
fi
for file in "${scripts[@]}"; do
sudo rm -f ~mc/"$file"
done
for file in "${units[@]}"; do
sudo rm -f "/etc/systemd/system/$file"
done
echo '@@@ Disabled services are listed in ~mc/disabled_services.txt @@@'