linux启动脚本和service、chkconfig
来源:互联网 发布:淘宝上哪家买德军军品 编辑:程序博客网 时间:2024/05/29 03:35
转载: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)启动脚本
- #!/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启动脚本
- #!/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
(4)系统脚本httpd
还可查看分析其他的系统脚本mysqld rc.sysinit crond portmap nfs等。
- #!/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
service命令可以快速地开启和停止Linux中的服务程序,这在调试过程中非常有用.chkconfig命令可以快速地设置开机时需要自动启动的服务程序.
这两个命令的功能都可以通过其他方法实现,只不过有了它们之后更加方便,维护工作可以更加轻松.
service命令和进程管理脚本
在debian linux中如果用apt-get命令安装MySQL,nginx等服务程序,安装程序都会自动在/etc/init.d/目录中创建一个管理此服务进程用的shell脚本,如:
/etc/init
.d
/mysql
/etc/init
.d
/nginx
/etc/init
.d
/keepalived
这样就用可以用/etc/init.d/{脚本文件名} start 或 service {脚本文件名} start 来启动一个服务,如:
#启动mysql服务
/etc/init
.d
/mysql
start
service mysql start
#重新启动mysql服务
/etc/init
.d
/mysql
restart
service mysql restart
上面两个命令的效果是一样的,这样重启mysql或PHP-fpm的时候就不用每次都先把进程kill掉,然后写一大段路径和参数来启动服务了.只不过用service命令的话只要记住脚本文件名,不用写绝对地址,这样比较方便,默认的脚本文件名都是和服务程序的名字一样的.
如果自己编译安装php-fpm等服务程序,/etc/init.d目录中的这个脚本是需要自己加的.幸运的是很多服务程序的源码包中都附带了这个脚本如:
mysql5.6源码中的 support-files
/mysql
.server
php5.4源码中的 sapi
/fpm/init
.d.php-fpm
redis2.6源码中的 utils
/redis_init_script
chmod
+x
/etc/init
.d
/mysql
/etc/init
.d
/nginx
/etc/init
.d
/redis
有的时候需要修改脚本中的参数如路径名等才能顺利执行,需要简单调试一下.
这里需要注意一下service命令后面的参数
service命令后面的第一个参数,一定要和/etc/init.d中的脚本名字一样,要不然系统会报错,为了简单,可以直接命名成服务程序名字.
service命令的第二个参数,如start,stop,restart,它是传给进程管理脚本的.所以是否支持这个参数不是service命令决定的,是进程管理脚本决定的,使用不同的进程管理脚本,可以选择的命令参数和功能也是不同的.
进程管理脚本都会支持start和stop两个命令参数,还有可能支持其他的命令参数.一般可以用service {脚本名} 查看脚本的帮助信息,里面有支持的命令参数如:
root@lvmingming:
/etc/init
.d
# service mysql
Usage:
/etc/init
.d
/mysql
start|stop|restart|reload|force-reload|status
root@lvmingming:
/etc/init
.d
# service redis
Please use start or stop as first argument
root@lvmingming:
/etc/init
.d
# service memcached
Usage:
/etc/init
.d
/memcached
{start|stop|restart|force-reload|status}
如果需要编译安装服务程序,并且源码里没有进程管理脚本,如memcached.那只能上网查别人写好的脚本了.要不然自己写个进程管理脚本也不错~
chkconfig命令和/etc/rc.local脚本
chkconfig命令可以用来配置某一项服务是否开机自动启动,有些版本的linux需要自己手动安装这个命令,如debian linux中安装chkconfig命令:
apt-get
install
chkconfig
chkconfig命令同样要依赖放在/etc/init.d目录中的进程管理脚本.
chkconfig命令示例:
#以下脚本在debian linux中测试通过
chkconfig --help
#查看chkconfig命令语法
chkconfig -l
#查看所有服务程序在所有运行级别下的是否开机自动启动
chkconfig -l mysql
#查mysql服务在所有运行级别下的是否开机自动启动
chkconfig -a mysql
#在服务列表中添加新的服务mysql并设置成开机自动启动
chkconfig -d mysql
#取消mysql服务的开机自动启动设置
chkconfig -s mysql 2345
#让mysql在2345这几个运行级别下开机自动启动,如果某一运行级别下的链接没有正确添加,可以使用这个命令添加一下.
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自动启动的服务:
/etc/rcS
.d/
#开机后需要自动启动的一些基本服务
/etc/rc0
.d/
#运行模式0下需要启动的服务
/etc/rc1
.d/
#运行模式1下需要启动的服务
/etc/rc2
.d/
#运行模式2下需要启动的服务
/etc/rc3
.d/
#运行模式3下需要启动的服务
/etc/rc4
.d/
#运行模式4下需要启动的服务
/etc/rc5
.d/
#运行模式5下需要启动的服务
/etc/rc6
.d/
#运行模式6下需要启动的服务
debian linux的默认运行级别是2,看一下/etc/rc2.d/中的内容
root@lvmingming:~
# ls -l /etc/rc2.d/
total 4
-rw-r--r-- 1 root root 677 Nov 17 2012 README
lrwxrwxrwx 1 root root 17 May 30 2011 S01ipvsadm -> ..
/init
.d
/ipvsadm
lrwxrwxrwx 1 root root 17 Feb 6 13:45 S16apache2 -> ..
/init
.d
/apache2
lrwxrwxrwx 1 root root 20 Jun 10 16:31 S17keepalived -> ..
/init
.d
/keepalived
lrwxrwxrwx 1 root root 19 Jun 13 13:54 S17memcached -> ..
/init
.d
/memcached
lrwxrwxrwx 1 root root 15 Feb 6 13:45 S17mysql -> ..
/init
.d
/mysql
lrwxrwxrwx 1 root root 28 Feb 16 13:14 S17nagios-nrpe-server -> ..
/init
.d
/nagios-nrpe-server
lrwxrwxrwx 1 root root 15 Feb 6 13:45 S17nginx -> ..
/init
.d
/nginx
lrwxrwxrwx 1 root root 15 Feb 6 13:45 S17rsync -> ..
/init
.d
/rsync
lrwxrwxrwx 1 root root 16 Feb 6 13:45 S17vsftpd -> ..
/init
.d
/vsftpd
lrwxrwxrwx 1 root root 22 Apr 3 14:06 S18avahi-daemon -> ..
/init
.d
/avahi-daemon
lrwxrwxrwx 1 root root 15 Feb 6 13:45 S18exim4 -> ..
/init
.d
/exim4
lrwxrwxrwx 1 root root 18 Feb 6 13:45 S20rc.
local
-> ..
/init
.d
/rc
.
local
每个软链接的命名都是"大写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开头的脚本,这些脚本都是有执行顺序的,是按照数字的顺序来执行的。
- linux启动脚本和service、chkconfig
- linux启动脚本和service、chkconfig
- linux启动脚本和service、chkconfig
- linux启动脚本和service、chkconfig
- linux启动脚本和service、chkconfig
- php-fpm服务启动脚本 service chkconfig
- nginx 服务启动脚本 chkconfig service
- linux(centos)中tomcat开机启动 配置service和chkconfig
- linux启动脚本讲解+示例 (chkconfig)
- linux命令之service和chkconfig
- linux chkconfig和service 的区别
- linux开机启动服务和chkconfig使用方法
- linux开机启动服务和chkconfig使用方法
- linux开机启动服务和chkconfig使用方法
- linux开机启动服务和chkconfig使用方法
- linux开机启动服务和chkconfig使用方法
- linux 开机启动服务和 chkconfig 使用方法
- linux开机启动服务和chkconfig使用方法
- 封装数据到List集合和Map集合
- 进程与线程的一个简单解释
- 有关iOS同步
- 2015蓝桥杯第五题(九分组分数)
- React学习之进阶非JSX的痛处(十七)
- linux启动脚本和service、chkconfig
- 【Anaconda】 报错问题解决
- Ubuntu14.04中配置Samba服务
- HashMap实现原理分析及简单实现一个HashMap
- 拆轮子系列:拆 OkHttp
- GUI for git|SourceTree|入门基础
- OpenGL ES 着色器(shader)介绍(转)
- 工作日志-不使用ScrollView让TextView可滚动
- selenium+python3.6学习——第一个脚本