Skip to content

Commit 6eb4dc2

Browse files
committed
Update maintenance.sh
- Added status monitoring - Added nginx config check - Added exit banner
1 parent 9a5b0a4 commit 6eb4dc2

1 file changed

Lines changed: 87 additions & 26 deletions

File tree

maintenance.sh

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set -e
1111
######################################################################
1212

1313

14-
version='1.0.0'
14+
version='1.0.1'
1515
#------------------------------------------------------------------------------#
1616
#
1717
# MIT License
@@ -40,6 +40,7 @@ version='1.0.0'
4040
# Declare variables
4141
SERVICE_NAME=nginx.service
4242
maintenance_file_path=/etc/nginx/html/server-error-pages/_site
43+
GITHUB_REPO=https://github.com/tmiland/Nginx-Maintenance-Mode
4344
# Icons used for printing
4445
ARROW=''
4546
DONE=''
@@ -65,7 +66,49 @@ HIDDEN="\033[8m"
6566
server_name=`echo "$1"`
6667
toggle=`echo "$2"`
6768

68-
header() {
69+
# Show service status - @FalconStats
70+
show_status () {
71+
# Add more services if you want to monitor
72+
declare -a services=(
73+
"nginx"
74+
"httpd"
75+
)
76+
declare -a serviceName=(
77+
"Nginx"
78+
"Apache"
79+
)
80+
declare -a serviceStatus=()
81+
for service in "${services[@]}"
82+
do
83+
serviceStatus+=($(systemctl is-active "$service.service"))
84+
done
85+
echo ""
86+
for i in ${!serviceStatus[@]}
87+
do
88+
if [[ "${serviceStatus[$i]}" == "active" ]]; then
89+
line+="${GREEN}${NC}${serviceName[$i]}: ${GREEN}${serviceStatus[$i]}${NC} "
90+
else
91+
line+="${serviceName[$i]}: ${RED}${serviceStatus[$i]}${NC} "
92+
fi
93+
done
94+
echo -e "$line"
95+
}
96+
97+
SHOW_STATUS=$(show_status)
98+
99+
# Check input
100+
check_input() {
101+
echo ""
102+
echo -e "${ORANGE}${INVERT}${WARNING}${BOLD} Nginx Maintenance Mode ${NC}"
103+
echo ""
104+
echo -e "${ORANGE}${ARROW} Usage:${NC}${GREEN} ./maintenance.sh [hostname] [on/off] ${NC}"
105+
echo ""
106+
}
107+
108+
INPUT_CHECK=$(check_input)
109+
110+
# Exit Script
111+
exit_script () {
69112
printf "${GREEN}"
70113
cat << "EOF"
71114
_ __ _
@@ -82,24 +125,39 @@ header() {
82125
83126
EOF
84127
printf "${NC}"
128+
echo -e "
129+
This script is fueled by coffee ☕
130+
131+
${GREEN}${DONE}${NC} ${BBLUE}Paypal${NC} ${ARROW} ${ORANGE}https://paypal.me/milanddata${NC}
132+
${GREEN}${DONE}${NC} ${BBLUE}BTC${NC} ${ARROW} ${ORANGE}3MV69DmhzCqwUnbryeHrKDQxBaM724iJC2${NC}
133+
${GREEN}${DONE}${NC} ${BBLUE}BCH${NC} ${ARROW} ${ORANGE}qznnyvpxym7a8he2ps9m6l44s373fecfnv86h2vwq2${NC}
134+
"
135+
echo -e "Documentation for this script is available here: ${ORANGE}\n${ARROW} ${GITHUB_REPO}${NC}\n"
136+
echo -e "${ORANGE}${ARROW} Goodbye.${NC}"
137+
echo ""
138+
exit
139+
}
140+
141+
header() {
85142
printf "${BLUE}"
86143
cat << EOF
144+
87145
╔═══════════════════════════════════════════════════════════════════╗
146+
║ ║
88147
║ Nginx Maintenance mode ║
89148
║ ║
90149
║ Easily toggle on or off maintenance mode with nginx ║
91150
║ ║
92-
║ Maintained by @tmiland ║
151+
║ Version: $version Maintained by @tmiland ║
152+
║ ║
93153
╚═══════════════════════════════════════════════════════════════════╝
94-
95154
EOF
96155
printf "${NC}"
97156
echo ""
98-
echo -e "Documentation for this script is available here: ${ORANGE}\n ${ARROW} https://github.com/tmiland/Nginx-Maintenance-Mode${NC}\n"
157+
echo -e "Documentation for this script is available here: ${ORANGE}\n ${ARROW} ${GITHUB_REPO}${NC}\n"
99158
}
100-
##
159+
101160
# Make sure that the script runs with root permissions
102-
##
103161
chk_permissions () {
104162
if [[ "$EUID" != 0 ]]; then
105163
echo -e "${RED}${ERROR} This action needs root permissions.${NC} Please enter your root password...";
@@ -110,56 +168,58 @@ chk_permissions () {
110168
exit 0;
111169
fi
112170
}
113-
##
171+
114172
# Make sure the maintenance file path exists
115-
##
116173
function checkDirExists() {
117174
if [ ! -d "$maintenance_file_path" ]
118175
then
119176
echo "Cannot find $maintenance_file_path."
120177
exit 1
121178
fi
122179
}
123-
##
180+
124181
# Check if maintenance mode is off
125-
##
126182
function checkToggleOn() {
127183
if [ ! -e "$maintenance_file_path/$server_name-maintenance-page_on.html" ]
128184
then
129-
echo -e "${RED}${ERROR} Maintenance mode is already off ${NC}"
185+
echo -e "${RED}${ERROR} Maintenance mode is already off for $server_name ${NC}"
186+
echo -e "${SHOW_STATUS} \n"
130187
exit 1
131188
fi
132189
}
133-
##
190+
134191
# Check if maintenance mode is on
135-
##
136192
function checkToggleOff() {
137-
if [ ! -e "$maintenance_file_path/maintenance-page_off.html" ]
193+
if [ -e "$maintenance_file_path/$server_name-maintenance-page_on.html" ]
138194
then
139-
echo -e "${RED}${ERROR} Maintenance mode is already on ${NC}"
195+
echo -e "${RED}${ERROR} Maintenance mode is already on for $server_name ${NC}"
196+
echo -e "${SHOW_STATUS} \n"
140197
exit 1
141198
fi
142199
}
143200

201+
# only runs if nginx -t succeeds
202+
safe_reload() {
203+
nginx -t &&
204+
systemctl reload $SERVICE_NAME
205+
}
206+
144207
# Restart Nginx
145208
restartNginx () {
146-
printf "\n-- ${GREEN}${ARROW} restarting Nginx\n ${NC}"
147-
${SUDO} systemctl restart $SERVICE_NAME
209+
printf "\n-- ${GREEN}${ARROW} reloading Nginx\n\n ${NC}"
210+
safe_reload
148211
sleep 2
149-
${SUDO} systemctl status $SERVICE_NAME --no-pager
212+
echo -e "${SHOW_STATUS} "
213+
#systemctl show -p SubState --value $SERVICE_NAME
150214
printf "\n"
151-
echo -e "${GREEN}${DONE} Nginx has been restarted ${NC}"
215+
echo -e "${GREEN}${DONE} Nginx has been reloaded ${NC}"
152216
sleep 3
153217
}
154218

155219
#check command input
156220
if [[ -z "$1" && -z "$2" ]];
157221
then
158-
echo -e "${ORANGE}${INVERT}${WARNING}${BOLD} Nginx Maintenance Mode ${NC}"
159-
echo ""
160-
echo ""
161-
echo -e "${ORANGE}${ARROW} Usage:${NC}${GREEN} ./maintenance.sh [hostname] [on/off] ${NC}"
162-
echo ""
222+
echo -e "${INPUT_CHECK}"
163223
exit 0
164224
fi
165225
main() {
@@ -186,9 +246,10 @@ elif [ "$2" == "off" ]
186246
echo -e "${GREEN}${DONE} Maintenance mode has been disabled ${NC}"
187247
restartNginx
188248
else
189-
echo "No command found."
249+
echo -e "${INPUT_CHECK}"
190250
fi
191251
}
192252

193253
header
194254
main $@
255+
exit_script

0 commit comments

Comments
 (0)