#Debian服务脚本 这里一redis为例,脚本文件位于/etc/init.d/redis-server:ios
<!-- 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描述以下:redis
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.shell
简单的说服务经过start-stop-daemon启动能够更便于系统管理。bash
脚本分析less
首先开头:### BEGIN INIT INFO ### END INIT INFO
之间的东西是必须的。这段内容描述了服务的一些信息。ide
而后是变量声明:并非强制的,只是规范,通常会声明服务的可执行文件,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。ui
服务管理:sysinitv本质上是BSD风格的init+shell。因此每一个发行版采用了不一样的机制去管理系统服务,其实本质上是将脚本根据运行级别分别链接到软链接到/etc/rc.X/下。在Debian下使用的是update-rc.d
命令。将上述脚本命名为redis,放到/etc/init.d/下。rest
二、增长一个服务
若是你想从新添加这个服务并让它开机自动执行,你须要执行如下命令: 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 显示运行过程信息