rewrite server script for cjserver

This commit is contained in:
wyy 2014-04-11 15:08:24 +08:00
parent 0d9008df7c
commit 0af9ae3de3
7 changed files with 24 additions and 90 deletions

View File

@ -47,9 +47,9 @@ make test
```
#Usage: /etc/init.d/cjserver {start|stop|restart|force-reload}
#启动
sudo /etc/init.d/cjserver start
/etc/init.d/cjserver.start
#停止
sudo /etc/init.d/cjserver stop
/etc/init.d/cjserver.stop
```
#### 测试服务

View File

@ -4,14 +4,8 @@
port=11200
#deamonize
daemonize=true
#dict path
dict_path=/usr/share/CppJieba/dict/jieba.dict.utf8
#model path
model_path=/usr/share/CppJieba/dict/hmm_model.utf8
#pid file
pid_file=/var/run/CppJieba/cjserver.pid

View File

@ -1,2 +1,2 @@
INSTALL(PROGRAMS cjserver DESTINATION /etc/init.d/)
INSTALL(PROGRAMS cjserver.start cjserver.stop DESTINATION /etc/init.d/)
INSTALL(PROGRAMS cjseg.sh DESTINATION bin)

View File

@ -1,63 +0,0 @@
#! /bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/cjserver
DAEMON_ARGS=/etc/CppJieba/server.conf
NAME=cjserver
DESC=cjserver
RUNDIR=/var/run/CppJieba
PIDFILE=$RUNDIR/cjserver.pid
LOGFILE=/dev/null
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chmod 755 $RUNDIR
if start-stop-daemon --start --quiet --pidfile $PIDFILE --exec /bin/bash -- -c "$DAEMON $DAEMON_ARGS >> $LOGFILE 2>&1"
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/QUIT/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
;;
restart|force-reload)
${0} stop
${0} start
;;
status)
echo -n "$DESC is "
if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
then
echo "running"
else
echo "not running"
exit 1
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0

10
script/cjserver.start Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
PID=`pidof cjserver`
if [ ! -z "${PID}" ]
then
echo "please stop cjserver first."
else
cjserver /etc/CppJieba/server.conf &
echo "service startted."
fi

11
script/cjserver.stop Executable file
View File

@ -0,0 +1,11 @@
#!/bin/sh
PID=`pidof cjserver`
if [ ! -z "${PID}" ]
then
kill ${PID}
sleep 1
echo "service stop ok."
else
echo "cjserver is not running."
fi

View File

@ -77,24 +77,6 @@ bool run(int argc, char** argv)
LogFatal("conf get model_path failed.");
return false;
}
if(conf.get("daemonize", val) && "true" == val)
{
if(fork() > 0)
exit(0);
setsid();
if(!conf.get("pid_file", val))
{
LogFatal("conf get pid_file failed.");
return false;
}
int pid = getpid();
string pidStr;
string_format(pidStr, "%d", pid);
loadStr2File(val.c_str(), ios::out, pidStr);
LogInfo("write pid[%s] into file[%s]", pidStr.c_str(), val.c_str());
}
ReqHandler reqHandler(dictPath, modelPath);
EpollServer sf(port, &reqHandler);