forked from danodemano/monitoring-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-daemon
133 lines (112 loc) · 2.27 KB
/
example-daemon
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
#!/bin/bash
# chkconfig: 2345 90 90
# description: custom-esxi
### BEGIN INIT INFO
# Provides: custom-esxi
# Required-Start: network
# Required-Stop: network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the program
### END INIT INFO
### Fill in these bits:
START_CMD="/var/www/scripts/esxmon.sh > /var/log/custom-esxi.log 2>&1 &"
NAME="custom-esxi"
PGREP_STRING="/var/www/scripts/esxmon.sh"
PID_FILE="/var/run/custom-esxi/custom-esxi.pid"
USER="root"
### No further muckin' about needed!
#CUR_USER=`whoami`
CUR_USER="root"
killproc() {
pkill -u $USER -f $PGREP_STRING
}
start_daemon() {
eval "$*"
}
log_success_msg() {
echo "$*"
logger "$_"
}
log_failure_msg() {
echo "$*"
logger "$_"
}
check_proc() {
pgrep -u $USER -f $PGREP_STRING >/dev/null
}
start_script() {
if [ "${CUR_USER}" != "root" ] ; then
log_failure_msg "$NAME can only be started as 'root'."
exit -1
fi
check_proc
if [ $? -eq 0 ]; then
log_success_msg "$NAME is already running."
exit 0
fi
[ -d /var/run/$NAME ] || (mkdir /var/run/$NAME )
# make go now
start_daemon /bin/su $USER -c "$START_CMD"
# Sleep for a while to see if anything cries
sleep 5
check_proc
if [ $? -eq 0 ]; then
log_success_msg "Started $NAME."
else
log_failure_msg "Error starting $NAME."
exit -1
fi
}
stop_script() {
if [ "${CUR_USER}" != "root" ] ; then
log_failure_msg "You do not have permission to stop $NAME."
exit -1
fi
check_proc
if [ $? -eq 0 ]; then
killproc -p $PID_FILE >/dev/null
# Make sure it's dead before we return
until [ $? -ne 0 ]; do
sleep 1
check_proc
done
check_proc
if [ $? -eq 0 ]; then
log_failure_msg "Error stopping $NAME."
exit -1
else
log_success_msg "Stopped $NAME."
fi
else
log_failure_msg "$NAME is not running or you don't have permission to stop it"
fi
}
check_status() {
check_proc
if [ $? -eq 0 ]; then
log_success_msg "$NAME is running."
else
log_failure_msg "$NAME is stopped."
exit -1
fi
}
case "$1" in
start)
start_script
;;
stop)
stop_script
;;
restart)
stop_script
start_script
;;
status)
check_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0