-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipfloaterd
executable file
·170 lines (155 loc) · 3.75 KB
/
ipfloaterd
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
164
165
166
167
168
169
170
#!/bin/bash
#
# /etc/rc.d/init.d/ipfloaterd
#
# Starts IPFloater daemon
#
# chkconfig: 345 65 35
# description: Starts the IPFloater daemon
# processname: ipfloaterd
### BEGIN INIT INFO
# Provides: IPFloater
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop ipfloater
# Description: start and stop ipfloater
### END INIT INFO
APPLICATION=IPFloater
LOGFILE=/var/log/ipfloater/ipfloater.log
PIDFILE=/var/lib/ipfloater/ipfloater.pid
APP=ipfloaterdaemon
DAEMON=$(which $APP)
if [ "$DAEMON" == "" ]; then
[ -e /usr/bin/$APP ] && DAEMON=/usr/bin/$APP
[ -e /usr/local/bin/$APP ] && DAEMON=/usr/local/bin/$APP
[ -e ./$APP ] && DAEMON=./$APP
fi
if [ "$DAEMON" == "" ]; then
echo "could not find the $APP daemon"
exit 1
fi
function status {
if [ $1 == '-f' ]; then
if [ ! -e "$2" ]; then
echo "could not find the pid file $2... maybe it is not started?"
return 0
fi
PID=$(cat $2 | head -n 1 | awk '{print $1}')
else
PID=$1
fi
CHECK=$(ps --pid "$PID")
if [ $? == 0 ]; then
echo $PID
return 1
else
echo "process not found"
return 0
fi
}
function stop {
PID=$(status -f $PIDFILE)
if [ $? == 1 ]; then
kill $PID
if [ $? == 0 ]; then
# echo "process killed... deleting pid file"
rm -f $PIDFILE
return 1
else
echo "could not kill the process ($PID)"
return 0
fi
return 1
else
echo "could not find the IPFloater server ($PID)"
return 0
fi
}
function start {
if [ -e "$PIDFILE" ]; then
echo "pid file $PIDFILE exists... checking if $APP is running"
PID=$(status -f $PIDFILE)
if [ $? == 1 ]; then
echo "$APP is already running"
return 0
else
echo "assuming that $APP is not running"
rm -f "$PIDFILE"
fi
fi
DIRLOG=$(dirname "$LOGFILE")
if [ "$DIRLOG" == "" ]; then
LOGFILE=/var/log/ipfloater/ipfloater.log
DIRLOG=/var/log/ipfloater
fi
[ ! -d "$DIRLOG" ] && mkdir -p "$DIRLOG"
/usr/bin/python $DAEMON > /dev/null 2>> "$LOGFILE" &
PID=$!
sleep 2
status $PID > /dev/null
if [ $? == 1 ]; then
DIRPID=$(dirname $PIDFILE)
if [ ! -d "$DIRPID" ]; then
mkdir -p "$DIRPID"
[ $? -ne 0 ] && echo "could not create directory for pid file, check permissions for dir $DIRPID"
fi
echo $PID > $PIDFILE
[ $? -ne 0 ] && echo "could not create pid file $PIDFILE, check permissions"
return 1
else
return 0
fi
}
function start_msg {
RESULT=$(start)
if [ $? == 1 ]; then
echo "** $APPLICATION successfully started"
return 0
else
echo "** error starting $APPLICATION. Please check logs or try launching daemon with the next command: \"/usr/bin/python $DAEMON\""
echo "$RESULT"
return 1
fi
}
function stop_msg {
RESULT=$(stop)
if [ $? == 1 ]; then
echo "** $APPLICATION stopped successfully"
return 0
else
echo "** error stopping $APPLICATION. Please check logs"
echo "$RESULT"
return 1
fi
}
case "$1" in
start)
start_msg
exit $?
;;
stop)
stop_msg
exit $?
;;
restart)
stop_msg
[ $? == 0 ] && sleep 2 && start_msg
exit $?
;;
status)
PID=$(status -f $PIDFILE)
if [ $? == 1 ]; then
echo "process running at pid $PID"
else
echo $PID
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
exit 0