您的位置:首页 > 运维架构 > Linux

linux启动脚本和service、chkconfig

2017-03-20 11:17 399 查看
转载:http://blog.csdn.net/taiyang1987912/article/details/41698817

(1)启动脚本的写法

case "$1" in
start)
do start-thing;
;;
stop)
do stop-thing;
;;
restart)
do restart-thing;
;;
...
esac

       你如果按这种结构写个启动脚本测试一下就会发现,若该脚本的软连接以S开头,则系统启动的时候执行start-thing代码段,若该脚本的软连接以K开头的话,则系统启动时执行stop-thing代码段(有一点儿例外的是运行级别0,因为0级别是关闭系统,所以/etc/rc0.d/下的以S开头的软连接也执行stop-thing段)。是不是很智能化,所以如果可以的话启动脚本最好写标准一些。如果你非写成普通的脚本,就几个命令,没有上面那个结构,建立以K或S开头的软连接会怎么执行呢?答案是全部执行。
chkconfig --add script
chkconfig --levels 2345 script on或chkconfig --levels 2345 script off

(2)启动脚本

[html] view plain copy #!/bin/sh -e  
PATH=/bin  
case "$1" in  
start)  
echo "starting"  
cd /usr/bin  
ls  
;;  
stop)  
echo "Stopping"  
kill -9  
;;  
restart)  
$0 stop || true  
$0 start  
;;  
*)  
echo "Usage: ls {start|stop|restart}"  
exit 1  
;;  
esac  
(3)自已动手开发apache启动脚本

[html] view plain copy



#!/bin/bash -e  
  
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions  
RETVAL=0  
httpd="/application/apache/bin/httpd"  
start() {  
        $httpd -k start >/dev/null 2>&1  
        # daemon httpd >/dev/null 2>&1  
        RETVAL=$?  
        [ $RETVAL -eq 0 ] && action "启动 httpd:" /bin/true ||\  
        action "启动 httpd:" /bin/false  
        return $RETVAL  
}  
  
stop() {  
        $httpd -k stop >/dev/null 2>&1  
        # killproc httpd >/dev/null 2>&1  
        [ $? -eq 0 ] && action "停止 httpd:" /bin/true ||\  
        action "停止 httpd:" /bin/false  
        return $RETVAL  
}  
case "$1" in  
  start)  
        start  
        ;;  
  stop)  
        stop  
        ;;  
  restart)  
         
       sh $0 stop  
       sh $0 start  
        ;;  
   *)  
        echo "Format error!"  
        echo $"Usage: $0 {start|stop|restart}"  
        exit 1  
        ;;  
esac  
exit $RETVAL  
有时使用#!/bin/bash -e表示脚本发生第一个错误时就中止脚本运行,对于那些不想终止的命令使用$0 stop || true(4)系统脚本httpd
还可查看分析其他的系统脚本mysqld rc.sysinit crond portmap nfs等。

[html] view plain copy



#!/bin/bash  
#  
# httpd        Startup script for the Apache HTTP Server  
#  
# chkconfig: - 85 15  
# description: The Apache HTTP Server is an efficient and extensible  \  
#          server implementing the current HTTP standards.  
# processname: httpd  
# config: /etc/httpd/conf/httpd.conf  
# config: /etc/sysconfig/httpd  
# pidfile: /var/run/httpd/httpd.pid  
#  
### BEGIN INIT INFO  
# Provides: httpd  
# Required-Start: $local_fs $remote_fs $network $named  
# Required-Stop: $local_fs $remote_fs $network  
# Should-Start: distcache  
# Short-Description: start and stop Apache HTTP Server  
# Description: The Apache HTTP Server is an extensible server   
#  implementing the current HTTP standards.  
### END INIT INFO  
  
# Source function library.  
. /etc/rc.d/init.d/functions  
  
if [ -f /etc/sysconfig/httpd ]; then  
        . /etc/sysconfig/httpd  
fi  
  
# Start httpd in the C locale by default.  
HTTPD_LANG=${HTTPD_LANG-"C"}  
  
# This will prevent initlog from swallowing up a pass-phrase prompt if  
# mod_ssl needs a pass-phrase from the user.  
INITLOG_ARGS=""  
  
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server  
# with the thread-based "worker" MPM; BE WARNED that some modules may not  
# work correctly with a thread-based MPM; notably PHP will refuse to start.  
  
# Path to the apachectl script, server binary, and short-form for messages.  
apachectl=/usr/sbin/apachectl  
httpd=${HTTPD-/usr/sbin/httpd}  
prog=httpd  
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}  
lockfile=${LOCKFILE-/var/lock/subsys/httpd}  
RETVAL=0  
STOP_TIMEOUT=${STOP_TIMEOUT-10}  
  
# The semantics of these two functions differ from the way apachectl does  
# things -- attempting to start while running is a failure, and shutdown  
# when not running is also a failure.  So we just do it the way init scripts  
# are expected to behave here.  
start() {  
        echo -n $"Starting $prog: "  
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS  
        RETVAL=$?  
        echo  
        [ $RETVAL = 0 ] && touch ${lockfile}  
        return $RETVAL  
}  
  
# When stopping httpd, a delay (of default 10 second) is required  
# before SIGKILLing the httpd parent; this gives enough time for the  
# httpd parent to SIGKILL any errant children.  
stop() {  
    echo -n $"Stopping $prog: "  
    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd  
    RETVAL=$?  
    echo  
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}  
}  
reload() {  
    echo -n $"Reloading $prog: "  
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then  
        RETVAL=6  
        echo $"not reloading due to configuration syntax error"  
        failure $"not reloading $httpd due to configuration syntax error"  
    else  
        # Force LSB behaviour from killproc  
        LSB=1 killproc -p ${pidfile} $httpd -HUP  
        RETVAL=$?  
        if [ $RETVAL -eq 7 ]; then  
            failure $"httpd shutdown"  
        fi  
    fi  
    echo  
}  
  
# See how we were called.  
case "$1" in  
  start)  
    start  
    ;;  
  stop)  
    stop  
    ;;  
  status)  
        status -p ${pidfile} $httpd  
    RETVAL=$?  
    ;;  
  restart)  
    stop  
    start  
    ;;  
  condrestart|try-restart)  
    if status -p ${pidfile} $httpd >&/dev/null; then  
        stop  
        start  
    fi  
    ;;  
  force-reload|reload)  
        reload  
    ;;  
  graceful|help|configtest|fullstatus)  
    $apachectl $@  
    RETVAL=$?  
    ;;  
  *)  
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"  
    RETVAL=2  
esac  
  
exit $RETVAL  
(5)service和chkconfig命令
service命令可以快速地开启和停止Linux中的服务程序,这在调试过程中非常有用.chkconfig命令可以快速地设置开机时需要自动启动的服务程序.
  这两个命令的功能都可以通过其他方法实现,只不过有了它们之后更加方便,维护工作可以更加轻松.

service命令和进程管理脚本

  在debian linux中如果用apt-get命令安装MySQL,nginx等服务程序,安装程序都会自动在/etc/init.d/目录中创建一个管理此服务进程用的shell脚本,如:

  这样就用可以用/etc/init.d/{脚本文件名} start service {脚本文件名} start 来启动一个服务,如:
  如果进程管理脚本支持restart命令参数,还可以用 /etc/init.d/{脚本文件名} restart service{脚本文件名} restart来重新启动 一个服务,如:
  上面两个命令的效果是一样的,这样重启mysql或PHP-fpm的时候就不用每次都先把进程kill掉,然后写一大段路径和参数来启动服务了.只不过用service命令的话只要记住脚本文件名,不用写绝对地址,这样比较方便,默认的脚本文件名都是和服务程序的名字一样的.
  如果自己编译安装php-fpm等服务程序,/etc/init.d目录中的这个脚本是需要自己加的.幸运的是很多服务程序的源码包中都附带了这个脚本如:
这些就是应用程序官方提供的进程管理脚本,把它们复制到/etc/init.d目录中,顺便改一个简短点儿的名字,就可以用service 命令管理服务进程了.别忘了给复制到/etc/init.d中的脚本加可执行权限.
  有的时候需要修改脚本中的参数如路径名等才能顺利执行,需要简单调试一下.
  这里需要注意一下service命令后面的参数
  service命令后面的第一个参数,一定要和/etc/init.d中的脚本名字一样,要不然系统会报错,为了简单,可以直接命名成服务程序名字.
  service命令的第二个参数,如start,stop,restart,它是传给进程管理脚本的.所以是否支持这个参数不是service命令决定的,是进程管理脚本决定的,使用不同的进程管理脚本,可以选择的命令参数和功能也是不同的.
  进程管理脚本都会支持start和stop两个命令参数,还有可能支持其他的命令参数.一般可以用service {脚本名} 查看脚本的帮助信息,里面有支持的命令参数如:
  也有些服务,如用apt-get安装的keepalived的管理脚本,用这个方法查看不到帮助信息.想知道这个脚本里支持哪些命令参数,只能自己读一下脚本了,shell脚本的语法比较简单,还是挺容易看懂的.
  如果需要编译安装服务程序,并且源码里没有进程管理脚本,如memcached.那只能上网查别人写好的脚本了.要不然自己写个进程管理脚本也不错~

chkconfig命令和/etc/rc.local脚本

  chkconfig命令可以用来配置某一项服务是否开机自动启动,有些版本的linux需要自己手动安装这个命令,如debian linux中安装chkconfig命令: 
  如果没有chkconfig命令,想添加一项开机自动启动的服务,可以把开启服务的命令放进/etc/rc.local中,只不过用chconfig命令更加方便
  chkconfig命令同样要依赖放在/etc/init.d目录中的进程管理脚本.
  chkconfig命令示例:

chkconfig的原理

       chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。使用语法:
chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]chkconfig在没有参数运行时,显示用法。如果加上服务名,那么就检查这个服务是否在当前运行级启动。如果是,返回true,否则返回false。如果在服务名后面指定了on,off或者reset,那么chkconfi 会改变指定服务的启动信息。on和off分别指服务被启动和停止,reset指重置服务的启动信息,无论有问题的初始化脚本指定了什么。on和off开关,系统默认只对运行级3,4,5有效,但是reset可以对所有运行级有效。参数用法:
   --add  增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
   --del  删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。
   --level<等级代号>  指定读系统服务要在哪一个执行等级中开启或关毕。
      等级0表示:表示关机
      等级1表示:单用户模式
      等级2表示:无网络连接的多用户命令行模式
      等级3表示:有网络连接的多用户命令行模式
      等级4表示:不可用
      等级5表示:带图形界面的多用户模式
      等级6表示:重新启动
      需要说明的是,level选项可以指定要查看的运行级而不一定是当前运行级。对于每个运行级,只能有一个启动脚本或者停止脚本。当切换运行级时,init不会重新启动已经启动的服务,也不会再次去停止已经停止的服务。    chkconfig --list [name]:显示所有运行级系统服务的运行状态信息(on或off)。如果指定了name,那么只显示指定的服务在不同运行级的状态。
    chkconfig --add name:增加一项新的服务。chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。如有缺少,则会从缺省的init脚本自动建立。
    chkconfig --del name:删除服务,并把相关符号连接从/etc/rc[0-6].d删除。
    chkconfig [--level levels] name:设置某一服务在指定的运行级是被启动,停止还是重置。运行级文件:
每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用 - 代替运行级。第二行对服务进行描述,可以用\ 跨行注释。
例如,random.init包含三行:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation.使用范例:
chkconfig --list        #列出所有的系统服务
chkconfig --add httpd        #增加httpd服务
chkconfig --del httpd        #删除httpd服务
chkconfig --level httpd 2345 on        #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态
chkconfig --list        #列出系统所有的服务启动情况
chkconfig --list mysqld        #列出mysqld服务设置情况
chkconfig --level 35 mysqld on        #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭
chkconfig mysqld on        #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级如何增加一个服务:
1.服务脚本必须存放在/etc/ini.d/目录下;
2.chkconfig --add servicename
    在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;
3.chkconfig --level 35 mysqld on
    修改服务的默认启动等级。/etc目录下有一组rc开头目录,它们用来存放在各个运行级别下linux自动启动的服务:

  这些目录中除README说明文档之外放的都是些软链接(符号链接),这些链接指向各服务的进程管理脚本,而这些进程管理脚本都放在/etc/init.d目录中.
  debian linux的默认运行级别是2,看一下/etc/rc2.d/中的内容
  用chkconfig命令操作的添加或者删除开机自动启动服务程序,其实就是创建或删除这些目录中的软链接.
  每个软链接的命名都是"大写S+运行顺序+脚本名称",里面有一个链接是"S20rc.local -> ../init.d/rc.local".打开它指向的脚本/etc/init.d/rc.local看一下,发现它里面调用了/etc/rc.local这个脚本.原来/etc/rc.local中的命令是在这儿执行的.
  如果把mysql服务的启动命令放进/etc/rc.local中,操作系统也会在执行这个列表的时候执行它,不过它的执行顺序是20,比排在前面的S17mysql还要晚一些~
chconfig --level 2345 puppet off 等价于 chkconfig puppet off (不用--level 指定级别时,默认是2345)
这条命令是设置 puppet 服务在 2345 级别 不自动启动,当执行这条命令时,会在
rc2.d rc3.d rc4.d rc5.d 这几个目录新建软连接 K02puppet 从 /etc/rc.d/init/puppet 脚本
chkconfig puppet on 这个就是在2345级别时,开机自动启动
当执行这条命令时,会在
rc2.d rc3.d rc4.d rc5.d 这几个目录新建软连接 S98puppet 从 /etc/rc.d/init/puppet 脚本
那这个 K02puppet S98puppet 中的 02 和 98 是怎么来的?
打开/etc/rc.d/init.d/puppet这个脚本文件,看前几行 如下:
#!/bin/bash
# puppet        Init script for running the puppet client daemon
#
# Author:       Duane Griffin
#               David Lutterkort
#
# chkconfig: - 98 02
#
# description: Enables periodic system configuration checks through puppet.
# processname: puppet
# config: /etc/sysconfig/puppet
特别是这行
# chkconfig: - 98 02
chkconfig 命令会读取这一行,来获取运行级别和那2个数字的,在这个脚本里面
-     是代表默认的级别(2345),也可以把想要启动的级别写到这里
98    代表的是在开机启动脚本中的启动顺序(第98个启动)
02    代表是 第二个停止的
98和02 都是代表的顺序,所以在编写服务启动脚本时这行是必不可少的,当然# description: 和 # processname:也是不能少的
在/etc/inittab 文件中 这一行
l3:3:wait:/etc/rc.d/rc 3
其中 /etc/rc.d/rc 这个脚本就是用来在 开机和关机时 管理服务的启动和停止的,当我们运行在3级别时,这个脚本会执行/etc/rc.d/rc3.d/下面所有脚本先执行K字头开头的脚本,然后执行S开头的脚本,这些脚本都是有执行顺序的,是按照数字的顺序来执行的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: