日期:2014-05-16  浏览次数:20635 次

编写服务程序的问题,谢谢
我写了一个守护程序,想在系统里面用服务脚本操作,写了启动脚本如下:


#!/bin/sh
# chkconfig: 2345 80 50
# description: myTestService is for testing how to write service in Linux
# processname: myTestService
# Source function library.
. /etc/rc.d/init.d/functions

tsd=${TSD-/usr/bin/tsd}
prog=tsd
pidfile=${PIDFILE-/var/run/tsd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/tsd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

start() {
        echo -n $"Starting $prog: "
        daemon --pidfile=${pidfile} $tsd

        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $tsd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}




# See how we were called.

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
status)
    status -p ${pidfile} $tsd
    RETVAL=$?
    ;;
*)
  echo $"Usage: $0 {start|stop|restart|status}"
  exit 1
esac

exit $ret


问题是我不知道守护进程启动后那个pid文件是怎么生成的,运行脚本之后,我的守护进程可以起来,但是/var/run下面没有相应pid文件,所以将服务stop的时候也会失败,没写过服务程序,求前辈指点,谢谢

------解决方案--------------------
你可以在“守护进程”这个程序里将pid记录到/var/run下的某个文件(xx.pid),stop时读取,然后删除