创建Linux系统服务的方法

(编辑:jimmy 日期: 2024/12/25 浏览:2)

linux环境:ubuntu

复制代码 代码如下:
#! /bin/sh
# Provides:       server.sh
# Default-Start:  2 3 4 5
# Short-Description: server testing...

EXEC_PATH=/home/huangmin/cdev/project
EXEC=server
PID_FILE=/var/run/server.sh.pid
DAEMON=/home/huangmin/cdev/project/server

if ! [ -x $EXEC_PATH/$EXEC ]; then
  echo "ERROR:$EXEC_PATH/$EXEC not found ..."
  exit 1
fi

stop() {
  echo "Stopping $EXEC ..."
  killall $DAEMON >/dev/null
  rm -f $PID_FILE
  sleep 1
  echo "Shutting down $EXEC: [OK]"
}

start() {
  echo "Starting $EXEC ..."
  $DAEMON >/dev/null
  pidof $EXEC > $PID_FILE
  sleep 1
  echo "Starting $EXEC: [OK]"
}

restart() {
  stop
  start
}

case "$1" in
  start)
    start

  stop)
    stop

  restart)
    restart

  status)
    echo "Status Here"

  *)
    echo "Usage: service $EXEC {start|stop|restart|status}"
    exit 1
esac

exit $?

/home/huangmin/cdev/project/server是我们需要的作为系统服务的程序。
写好脚本之后,将其放置到目录/etc/init.d中,或者做成软链接形式如:sudo ln -s /home/huangmin/cdev/project/bash/server.sh /etc/init.d/servertest.sh
最后利用chkconfig命令将其添加进系统服务: chkconfig –add servertest.sh
可以使用命令service servertest.sh start马上启动该服务程序