-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttserv_tw_ctl
executable file
·169 lines (149 loc) · 3.22 KB
/
ttserv_tw_ctl
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
#! /bin/sh
#----------------------------------------------------------------
# Startup script for the server of Tokyo Tyrant
#----------------------------------------------------------------
# configuration variables
prog="ttservctl"
cmd="ttserver"
basedir="/tmp/fs/db"
port="1984"
pidfile="$basedir/tw_tt_pid"
#logfile="$basedir/tw_tt_log"
#ulogdir="$basedir/tw_tt_ulog"
logfile="/dev/null"
ulogdir="/dev/null"
ulimsiz="512m"
#sid=1
#mhost="remotehost1"
#mport="1978"
#rtsfile="$basedir/rts"
dbname="$basedir/tw_tt.tct#bnum=15000#rcnum=2000"
maxcon="65535"
retval=0
# setting environment variables
LANG=C
LC_ALL=C
PATH="$PATH:/opt/local/bin:/sbin:/usr/sbin:/usr/local/sbin"
export LANG LC_ALL PATH
# start the server
start(){
printf 'Starting the server of Tokyo Tyrant\n'
mkdir -p "$basedir"
if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] || [ -z "$dbname" ] ; then
printf 'Invalid configuration\n'
retval=1
elif ! [ -d "$basedir" ] ; then
printf 'No such directory: %s\n' "$basedir"
retval=1
elif [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
printf 'Existing process: %d\n' "$pid"
retval=1
else
if [ -n "$maxcon" ] ; then
ulimit -n "$maxcon" >/dev/null 2>&1
fi
cmd="$cmd -port $port -dmn -pid $pidfile"
if [ -n "$logfile" ] ; then
cmd="$cmd -log $logfile"
fi
if [ -n "$ulogdir" ] ; then
mkdir -p "$ulogdir"
cmd="$cmd -ulog $ulogdir"
fi
if [ -n "$ulimsiz" ] ; then
cmd="$cmd -ulim $ulimsiz"
fi
if [ -n "$sid" ] ; then
cmd="$cmd -sid $sid"
fi
if [ -n "$mhost" ] ; then
cmd="$cmd -mhost $mhost"
fi
if [ -n "$mport" ] ; then
cmd="$cmd -mport $mport"
fi
if [ -n "$rtsfile" ] ; then
cmd="$cmd -rts $rtsfile"
fi
printf "Executing: %s\n" "$cmd"
cmd="$cmd $dbname"
$cmd
if [ "$?" -eq 0 ] ; then
printf 'Done\n'
else
printf 'The server could not started\n'
retval=1
fi
fi
}
# stop the server
stop(){
printf 'Stopping the server of Tokyo Tyrant\n'
if [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
printf "Sending the terminal signal to the process: %s\n" "$pid"
kill -TERM "$pid"
c=0
while true ; do
sleep 0.1
if [ -f "$pidfile" ] ; then
c=`expr $c + 1`
if [ "$c" -ge 100 ] ; then
printf 'Hanging process: %d\n' "$pid"
retval=1
break
fi
else
printf 'Done\n'
break
fi
done
else
printf 'No process found\n'
retval=1
fi
}
# send HUP to the server for log rotation
hup(){
printf 'Sending HUP signal to the server of Tokyo Tyrant\n'
if [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
printf "Sending the hangup signal to the process: %s\n" "$pid"
kill -HUP "$pid"
printf 'Done\n'
else
printf 'No process found\n'
retval=1
fi
}
# check permission
if [ -d "$basedir" ] && ! touch "$basedir/$$" >/dev/null 2>&1
then
printf 'Permission denied\n'
exit 1
fi
rm -f "$basedir/$$"
# dispatch the command
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
hup)
hup
;;
*)
printf 'Usage: %s {start|stop|restart|hup}\n' "$prog"
exit 1
;;
esac
# exit
exit "$retval"
# END OF FILE