把程序注册为自启动服务

以searchd程序为例:

程序所在位置为:/usr/local/coreseek/bin

程序名称为:searchd

程序运行标识保存在:/var/run/searchd.pid

要调用程序查询接口需要 启动searchd程序 下面把searchd注册为服务。

编写searchd的服务脚本并取名为searchd, 内容如下:

#!/bin/bash
#
# autoruntest  the shell script takes care of autoruntest auto start and stop
#
# chkconfig:   2345 20 80
# description: autoruntest
# processname:     /etc/rc.d/init.d/autoruntest

EXEC=searchd
PID_FILE=/var/run/searchd.pid
DAEMON=/usr/local/coreseek/bin/searchd

# Source function library.
. /etc/rc.d/init.d/functions

if ! [ -x $DAEMON ] ; then
       echo "ERROR: $DAEMON not found"
       exit 1
fi

stop()
{
       echo "Stoping $EXEC ..."
       killall $DAEMON >/dev/null
       usleep 100
       echo "Shutting down $EXEC: [  OK  ]"      
}

start()
{
       echo "Starting $EXEC ..."
       $DAEMON > /dev/null &
       usleep 100
       echo "Starting $EXEC: [  OK  ]"         
}

restart()
{
       stop
       start
}


case "$1" in
       start)
       start
       ;;
       stop)
       stop
       ;;
       restart)
       restart
       ;;
       status)
       status -p $PID_FILE $DAEMON  
       ;;    
  *)
       echo "Usage: service $EXEC {start|stop|restart|status}"
       exit 1
esac

exit $?

该服务的脚本文件拷入/etc/rc.d/init.d 文件夹下保证其状态为可执行!!!

添加服务

chkconfig --add searchd

服务设置为自启动

chkconfig --level 35 searchd on

启动服务

service searchd start