diff --git a/README.md b/README.md index 9c59fbdd..f1625f9b 100644 --- a/README.md +++ b/README.md @@ -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 ``` #### 测试服务 diff --git a/conf/server.conf b/conf/server.conf index 964a389b..c47e80e6 100644 --- a/conf/server.conf +++ b/conf/server.conf @@ -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 diff --git a/script/CMakeLists.txt b/script/CMakeLists.txt index 1772cffc..ff4f353f 100644 --- a/script/CMakeLists.txt +++ b/script/CMakeLists.txt @@ -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) diff --git a/script/cjserver b/script/cjserver deleted file mode 100755 index 85666f01..00000000 --- a/script/cjserver +++ /dev/null @@ -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 diff --git a/script/cjserver.start b/script/cjserver.start new file mode 100755 index 00000000..2205becb --- /dev/null +++ b/script/cjserver.start @@ -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 diff --git a/script/cjserver.stop b/script/cjserver.stop new file mode 100755 index 00000000..b6a43f03 --- /dev/null +++ b/script/cjserver.stop @@ -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 diff --git a/src/server.cpp b/src/server.cpp index 441b85c0..658db8e2 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -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);