Debian的服务脚本简析

#Debian服务脚本 这里一redis为例,脚本文件位于/etc/init.d/redis-server:

<!-- lang: shell -->
#! /bin/sh
### BEGIN INIT INFO
# Provides:		redis-server
# Required-Start:	$syslog $remote_fs
# Required-Stop:	$syslog $remote_fs
# Should-Start:		$local_fs
# Should-Stop:		$local_fs
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	redis-server - Persistent key-value db
# Description:		redis-server - Persistent key-value db
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/redis-server
DAEMON_ARGS=/etc/redis/redis.conf
NAME=redis-server
DESC=redis-server

RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis-server.pid                                                                                                                                                                                                              

test -x $DAEMON || exit 0

if [ -r /etc/default/$NAME ]
then
    . /etc/default/$NAME
fi

. /lib/lsb/init-functions

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    mkdir -p $RUNDIR
    touch $PIDFILE
    chown redis:redis $RUNDIR $PIDFILE
    chmod 755 $RUNDIR

    if [ -n "$ULIMIT" ]
    then
        ulimit -n $ULIMIT
    fi

    if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS
    then
        echo "$NAME."
    else
        echo "failed"
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: "
    if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
    then
        echo "$NAME."
    else
        echo "failed"
    fi
    rm -f $PIDFILE
    sleep 1
    ;;

  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|status}" >&2
    exit 1
    ;;
esac

exit 0

其实就是一个特殊的bash脚本,主要是通过start-stop-daemon来管理进程的,start-stop-daemon的manpage描述如下:

start-stop-daemon is used to control the creation and termination of system-level processes. Using one of the matching options, start-stop-daemon can be configured to find existing instances of a running process.
Note: unless --pidfile is specified, start-stop-daemon behaves similar to killall(1). start-stop-daemon will scan the process table looking for any processes which matchthe process name, uid, and/or gid (if specified). Any matching process will prevent --start from starting the daemon. All matching processes will be sent the TERM signal (or the one specified via --signal or --retry) if --stop is specified. For daemons which have long-lived children which need to live through a --stop, you must specify a pid‐file.

简单的说服务通过start-stop-daemon启动可以更便于系统管理。

  • 脚本分析

  • 首先开头:### BEGIN INIT INFO ### END INIT INFO之间的东西是必须的。这段内容描述了服务的一些信息。

  • 然后是变量声明:并不是强制的,只是规范,一般会声明服务的可执行文件,PID文件所在的位置等信息。

  • /etc/default/*.conf 加载默认配置 /etc/init.d/fuctions(Debian为/lib/lsb/init-fuctions) 为/etc/init.d中的脚本提供一些变量定义和实用函数,详细的可以参考fuctions.

  • 正文,定义start/stop/status/restart的行为,一般照着上面的例子就可以了,此外还可以参考start-stop-daemon的man page。

  • 服务管理:sysinitv本质上是BSD风格的init+shell。所以每个发行版采用了不同的机制去管理系统服务,其实本质上是将脚本根据运行级别分别连接到软连接到/etc/rc.X/下。在Debian下使用的是update-rc.d命令。将上述脚本命名为redis,放到/etc/init.d/下。

  1. 删除一个服务 update-rc.d -f redis remove 参数-f是强制删除符号链接,即使/etc/init.d/redis仍然存在。 Note:这个命令仅仅禁止该服务,直到该服务被升级。如果你想在服务升级后仍然保持被禁用。应该执行如下的命令: update-rc.d redis stop 80 0 1 2 3 4 5 6 .

2、增加一个服务

如果你想重新添加这个服务并让它开机自动执行,你需要执行以下命令: update-rc.d redis defaults 并且可以指定该服务的启动顺序: update-rc.d redis defaults 90 还可以更详细的控制start与kill顺序: update-rc.d redis defaults 20 80 其中前面的20是start时的运行顺序级别,80为kill时的级别。也可以写成: update-rc.d redis start 20 2 3 4 5 . stop 80 0 1 6 . 其中0~6为运行级别。 update-rc.d命令不仅适用Linux服务,编写的脚本同样可以用这个命令设为开机自动运行

附录:start-stop-daemon常用选项:

主要参数 Commands:
-S|--start -- <argument> ... 开启一个系统守护程序,并传递参数给它
-K|--stop 停止一个程序
-T|--status 得到程序的状态
-H|--help 显示帮助信息
-V|--version 打印版本信息
Matching options (at least one is required):
-p|--pidfile <pid-file> pid file to check
-x|--exec <executable> program to start/check if it is running
-n|--name <process-name> process name to check
-u|--user <username|uid> process owner to check
Options: -g|--group <group|gid> 按指定用户组权限运行程序
-c|--chuid <name|uid[:group|gid]>
按指定用户、用户组权限运行程序
-s|--signal <signal> signal to send (default TERM)
-a|--startas <pathname> program to start (default is <executable>)
-r|--chroot <directory> chroot to <directory> before starting
-d|--chdir <directory> change to <directory> (default is /)
-N|--nicelevel <incr> add incr to the process' nice level
-P|--procsched <policy[:prio]>
use <policy> with <prio> for the kernel
process scheduler (default prio is 0)
-I|--iosched <class[:prio]> use <class> with <prio> to set the IO
scheduler (default prio is 4)
-k|--umask <mask> 在开始运行前设置<mask>
-b|--background 后台运行
-m|--make-pidfile 当命令本身不创建pidfile时,由start-stop-daemon创建
-R|--retry <schedule> 等待timeout的时间,检查进程是否停止,如果没有发送KILL信号;
-t|--test 测试模式
-o|--oknodo exit status 0 (not 1) if nothing done
-q|--quiet 不要输出警告
-v|--verbose 显示运行过程信息

转载于:https://my.oschina.net/u/1242185/blog/365295

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值