forked from tbaldur/cyberpanel-mods
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_services.sh
56 lines (48 loc) · 1.49 KB
/
check_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
#!/bin/bash
# This script allows you to check if essential services are running, if any of the services is not running it attempts to start it.
# Add to crontab - 0 0 * * * /bin/bash /root/cyberpanel-mods/check_services.sh >/dev/null 2>&1
if [ ${EUID} -ne 0 ]; then
echo "Please run as root or sudo user"
exit 1
fi
# Set the log file path
LOG_FILE="/var/log/cyberp-services-check.log"
# Function to log messages
log_message() {
local timestamp="$(date +'%m.%d.%Y_%H-%M-%S')"
echo "[$timestamp] $1" >> "$LOG_FILE"
}
# List of services to check, add here
services=(
"pdns"
"lshttpd"
"lscpd"
"opendkim"
"mariadb.service"
"fail2ban.service"
"lsmcd"
"crowdsec"
"pure-ftpd"
"redis"
"fail2ban"
"crond"
"sshd"
"rsyslog"
)
for service in "${services[@]}"; do
if systemctl is-active --quiet "$service"; then
echo "$service is running."
log_message "$service is running."
else
echo "$service is not running. Starting $service..."
log_message "$service is not running. Attempting to start..."
start_output=$(systemctl start "$service" 2>&1)
if systemctl is-active --quiet "$service"; then
echo "$service has been started successfully."
log_message "$service has been started successfully."
else
echo "Failed to start $service. Reason: $start_output"
log_message "Failed to start $service. Reason: $start_output"
fi
fi
done