Skip to content

Commit a063461

Browse files
committed
add nutcracker control script.
1 parent 744b33e commit a063461

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed

bashrc/nutcracker_control

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
#!/bin/sh
2+
#
3+
# nutcracker_control <summary>
4+
# description: Starts and stops a single nutcracker instance on this system
5+
#
6+
7+
### BEGIN INIT INFO
8+
### END INIT INFO
9+
10+
#
11+
# init.d / servicectl compatibility (openSUSE)
12+
#
13+
if [ -f /etc/rc.status ]; then
14+
. /etc/rc.status
15+
rc_reset
16+
fi
17+
18+
#
19+
# Source function library.
20+
#
21+
if [ -f /etc/rc.d/init.d/functions ]; then
22+
. /etc/rc.d/init.d/functions
23+
fi
24+
25+
#set -x
26+
export PS4='+ [`basename ${BASH_SOURCE[0]}`:$LINENO ${FUNCNAME[0]} \D{%F %T} $$ ] '
27+
28+
MYNAME="${0##*/}"
29+
CURDIR=$(cd "$(dirname "$0")"; pwd)
30+
PROG="nutcracker"
31+
PIDFILE="/var/run/${PROG}.pid"
32+
LOGFILE="/var/log/${PROG}.log"
33+
EXEC="${CURDIR}/${PROG}"
34+
CONFFILE="${CURDIR}/conf/${PROG}.yml"
35+
ACTION=""
36+
DAEMON=1
37+
38+
[ -e /etc/sysconfig/$PROG ] && . /etc/sysconfig/$PROG
39+
40+
usage() {
41+
cat << USAGE
42+
Usage: bash ${MYNAME} [-h] [-c CONFIG] [-p PID] -d
43+
action {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
44+
45+
Nutcracker control scripts.
46+
47+
Optional arguments:
48+
-h, --help show this help message and exit.
49+
-d run program not in daemon mode(default: ${DAEMON}).
50+
-c CONFIG ini config file, default: ${CONFFILE}
51+
-p PID path to pid file, default: ${PIDFILE}
52+
53+
Require:
54+
action {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
55+
56+
USAGE
57+
58+
exit 1
59+
}
60+
61+
#
62+
# Parses command-line options.
63+
# usage: parse_options "$@" || exit $?
64+
#
65+
parse_options() {
66+
declare -a argv
67+
68+
while [[ $# -gt 0 ]]; do
69+
case $1 in
70+
-c)
71+
CONFFILE="${2}"
72+
shift 2
73+
;;
74+
-p|--pid)
75+
PIDFILE="${2}"
76+
shift 2
77+
;;
78+
-h|--help)
79+
usage
80+
exit
81+
;;
82+
-d)
83+
DAEMON=0
84+
shift
85+
;;
86+
--)
87+
shift
88+
argv=("${argv[@]}" "${@}")
89+
break
90+
;;
91+
-*)
92+
echo "command line: unrecognized option $1" >&2
93+
return 1
94+
;;
95+
*)
96+
argv=("${argv[@]}" "${1}")
97+
shift
98+
;;
99+
esac
100+
done
101+
102+
case ${#argv[@]} in
103+
1)
104+
ACTION="${argv[0]}"
105+
;;
106+
0|*)
107+
usage 1>&2
108+
return 1
109+
;;
110+
esac
111+
}
112+
113+
check_program() {
114+
115+
if [ ! -x "$EXEC" ]; then
116+
echo "Could not find any executable ${PROG}, please check it first."
117+
exit 1
118+
fi
119+
}
120+
121+
start() {
122+
check_program
123+
124+
echo -n $"Starting $PROG: "
125+
# if not running, start it up here, usually something like "daemon $exec"
126+
if [ x"$DAEMON" == "x0" ]; then
127+
daemon ${EXEC} -c ${CONFFILE} -p ${PIDFILE}
128+
else
129+
daemon ${EXEC} -c ${CONFFILE} -p ${PIDFILE} -d
130+
fi
131+
retval=$?
132+
echo
133+
#[ $retval -eq 0 ] && touch $lockfile
134+
return $retval
135+
}
136+
137+
stop() {
138+
echo -n $"Stopping $PROG: "
139+
# stop it here, often "killproc $PROG"
140+
killproc -p $PIDFILE -d 20 $PROG
141+
retval=$?
142+
echo
143+
#[ $retval -eq 0 ] && rm -f $lockfile
144+
return $retval
145+
}
146+
147+
pstatus () {
148+
if pgrep -f "${EXEC}" >/dev/null; then
149+
#echo "$PROG" running
150+
return 0
151+
fi
152+
return 1
153+
}
154+
155+
forcestop() {
156+
echo "Force stopping $PROG"
157+
pkill $1 -f "${EXEC}"
158+
}
159+
160+
forcerestart () {
161+
stop
162+
try=1
163+
sleep 1
164+
while pstatus; do
165+
try=$((try + 1))
166+
if [ $try -gt 3 ]; then
167+
forcestop -9
168+
else
169+
forcestop
170+
fi
171+
echo "Waiting for $PROG to die.."
172+
sleep 5
173+
done
174+
start
175+
}
176+
177+
restart() {
178+
forcerestart
179+
}
180+
181+
reload() {
182+
restart
183+
}
184+
185+
force_reload() {
186+
restart
187+
}
188+
189+
rh_status() {
190+
# run checks to determine if the service is running or use generic status
191+
status -p ${PIDFILE} ${PROG}
192+
}
193+
194+
rh_status_q() {
195+
rh_status >/dev/null 2>&1
196+
}
197+
198+
################################## main route #################################
199+
parse_options "${@}" || usage
200+
201+
case "${ACTION}" in
202+
start)
203+
rh_status_q && exit 0
204+
${ACTION}
205+
;;
206+
stop)
207+
rh_status_q || exit 0
208+
${ACTION}
209+
pstatus && forcestop -9
210+
;;
211+
restart)
212+
${ACTION}
213+
;;
214+
reload)
215+
rh_status_q || exit 7
216+
${ACTION}
217+
;;
218+
force-reload)
219+
force_reload
220+
;;
221+
status)
222+
rh_status
223+
;;
224+
condrestart|try-restart)
225+
rh_status_q || exit 0
226+
restart
227+
;;
228+
*)
229+
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
230+
exit 2
231+
esac
232+
exit $?
233+

0 commit comments

Comments
 (0)