-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstop_unwanted_apps.sh
executable file
·164 lines (149 loc) · 4.73 KB
/
stop_unwanted_apps.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
CONF_BASE=/etc/monit/conf.d
INIFILE=~root/stop_unwanted_apps.ini
if [[ "$1" == "--help" || "$1" == "-?" || "$1" == "-h" ]]; then
echo ""
echo " stop_unwanted_apps.sh [OPTION]"
echo ""
echo " Automates service actions based on entries in $INIFILE"
echo " Services are those defined in Monit service files in $CONF_BASE"
echo " -? : This help"
echo " -l : List out all the Monit services detected on this system"
echo " -q : Quiet"
echo ""
echo " Only one option can be specified due to my poor coding skills :)"
echo ""
exit 1
fi
if [[ ! -d $CONF_BASE ]]; then
echo ""
echo "ERROR: Monit config files not found in expected location: $CONF_BASE"
echo ""
exit 1
fi
if [[ "$1" == "-l" ]]; then
echo ""
echo "Listing Monit service definitions in $CONF_BASE:"
cd $CONF_BASE
for s in *; do
echo " $s"
done
echo ""
exit 2
fi
if [[ "$1" == "-q" ]]; then
VERBOSE=0
else
VERBOSE=1
fi
if [[ ! -f $INIFILE ]]; then
echo ""
echo "$INIFILE does not exist - Creating with defaults"
echo ""
cat << EOF >$INIFILE
# Actions should be one of;
# start Start service now
# autostart Start service and set to start on boot
# stop Stop service now
# disable Stop service now, and disable start-on-boot
# ignore Leave service as-is
# Any other action word, or not specifying an action, ignores the service and skips any action.
# Similarly, any Monit service without a section is ignored.
# If the Monit definition file has a different name to the formal systemctl service,
# use the servicename directive to override it.
# Example:
# [syncthing]
# action=disable
# servicename=syncthing@openflixr
EOF
cd $CONF_BASE
for s in *; do
if [[ "$s" == "localhost" || "$s" == "nfs" || "$s" == "ssh" || "$s" == "cron" || "$s" == "ntp" || "$s" == "_localsetup" || "$s" == "dnsmasq" || "$s" == "pihole" || "$s" == "webmin" || "$s" == "mysql" || "$s" == "nginx" || "$s" == "htpcmanager" ]]; then
echo " Skipping $s as it is a system component"
continue
fi
echo " Adding $s"
echo "[$s]">>$INIFILE
echo "action=ignore">>$INIFILE
case $s in
hass)
echo "servicename=home-assistant">>$INIFILE
;;
sabnzbd)
echo "servicename=sabnzbdplus">>$INIFILE
;;
syncthing)
echo "servicename=syncthing@openflixr">>$INIFILE
;;
nzbhydra)
echo "servicename=nzbhydra2">>$INIFILE
;;
plexms)
echo "servicename=plexmediaserver">>$INIFILE
;;
esac
echo "">>$INIFILE
done
echo ""
echo "*****************************************************************"
echo "Created default ini at $INIFILE"
echo "All actions have been set to 'ignore' initially."
echo "Please review and edit this file before running the script again."
echo "*****************************************************************"
echo ""
exit 2
fi
debug () {
if [[ $VERBOSE == 1 ]]; then
echo "$*"
fi
}
exec 1> >(tee -a /var/log/stop_unwanted_apps.log) 2>&1
TODAY=$(date)
debug "-----------------------------------------------------"
debug "Date: $TODAY"
debug "-----------------------------------------------------"
cd $CONF_BASE
for s in *; do
debug "Evaluating Monit service $s:"
ACTION=$(crudini --get $INIFILE $s action 2>/dev/null || echo "notdefined")
debug " Requested action is $ACTION"
case $ACTION in
start)
debug " Actioning 'monit start' on $s"
monit start $s
debug ""
;;
stop)
debug " Actioning 'monit stop' on $s"
monit stop $s
debug ""
;;
autostart)
debug " Actioning 'monit start' on $s"
monit start $s
SERVICENAME=$(crudini --get $INIFILE $s servicename 2>/dev/null || echo "$s")
if [[ "$SERVICENAME" != "$s" ]]; then
debug " Overriding service name as $SERVICENAME"
fi
debug " Actioning 'systemctl enable' on $SERVICENAME"
systemctl enable $SERVICENAME 2>/dev/null
debug ""
;;
disable)
debug " Actioning 'monit stop' on $s"
monit stop $s
SERVICENAME=$(crudini --get $INIFILE $s servicename 2>/dev/null || echo "$s")
if [[ "$SERVICENAME" != "$s" ]]; then
debug " Overriding service name as $SERVICENAME"
fi
debug " Actioning 'systemctl disable' on $SERVICENAME"
systemctl disable $SERVICENAME 2>/dev/null
debug ""
;;
*)
debug " Ignoring definition $s"
debug ""
;;
esac
done