你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux注册系统服务

[日期:2012-12-24] 来源:Linux社区  作者:firefoxbug [字体:大 中 小]

Linux下注册一个系统服务,开机自启动.

1、脚本编写

#vim test.sh

#!/bin/bash

#description: hello.sh
#chkconfig: 2345 20 81

EXEC_PATH=/usr/local/
EXEC=hello.sh
DAEMON=/usr/local/hello.sh
PID_FILE=/var/run/hello.sh.pid

. /etc/rc.d/init.d/functions

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

stop()
{
      echo "Stoping $EXEC ..."
      ps aux | grep "$DAEMON" | kill -9 `awk '{print $2}'` >/dev/null 2>&1
      rm -f $PID_FILE
      usleep 100
      echo "Shutting down $EXEC: [  OK  ]"   
}

start()
{
      echo "Starting $EXEC ..."
      $DAEMON > /dev/null &
      pidof $EXEC > $PID_FILE
      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 $?

2、注册服务

# chmod 700 test.sh

# cp test.sh /etc/init.d/

# chkconfig --add test.sh

# chkconfig --list

3、删除服务

# chkconfig  --del test.sh

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款