forked from sierracircle/services-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services
executable file
·87 lines (63 loc) · 2.46 KB
/
services
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
#!/bin/bash
#ver. 4
##this script will check mysql and apache
##if that service is not running
##it will start the service and send an email to you
##if the restart does not work, it sends an email and then exits
##set the path ##this works for Ubuntu 14.04 and 16.04
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
##set your email address
EMAIL="email@yourdomain.com"
##list your services you want to check
SERVICES=( 'apache2' 'mysql' )
#### OK. STOP EDITING ####
for i in "${SERVICES[@]}"
do
echo "checking $i service"
###CHECK SERVICE####
`pgrep $i >/dev/null 2>&1`
STATS=$(echo $?)
###IF SERVICE IS NOT RUNNING####
if [[ $STATS == 1 ]]
then
echo "$i service is not running - starting"
##TRY TO RESTART THAT SERVICE###
service $i stop
service $i start
##CHECK IF RESTART WORKED###
`pgrep $i >/dev/null 2>&1`
RESTART=$(echo $?)
if [[ $RESTART == 0 ]]
##IF SERVICE HAS BEEN RESTARTED###
then
echo "$i service start worked..."
##REMOVE THE TMP FILE IF EXISTS###
if [ -f "/tmp/$i" ];
then
rm /tmp/$i
fi
##SEND AN EMAIL###
MESSAGE="$i was down, but I was able to restart it on $(hostname) $(date) "
SUBJECT="$i was down -but restarted- on $(hostname) $(date) "
#echo $MESSAGE | mail -s "$SUBJECT" "$EMAIL"
#curl --request POST \
# --url https://api.sendgrid.com/v3/mail/send \
# --header 'Authorization: Bearer YOUR_API_KEY' \
# --header 'Content-Type: application/json' \
# --data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'
else
##IF RESTART DID NOT WORK###
echo "$i start did NOT work... :("
##CHECK IF THERE IS NOT A TMP FILE###
if [ ! -f "/tmp/$i" ]; then
##CREATE A TMP FILE###
touch /tmp/$i
##SEND A DIFFERENT EMAIL###
MESSAGE="$i is down on $(hostname) at $(date) "
SUBJECT=" $i down on $(hostname) $(date) "
echo $MESSAGE " I tried to restart it, but it did not work" | mail -s "$SUBJECT" "$EMAIL"
fi
fi
fi
done
exit 0;