nginx脚本编写

编写nginx状态脚本

#! /bin/bash
#chkconfig: 2345 99 99               
#description: this is nginx jb
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0{start | stop | restart | reload}"
exit 1
;;
esac
exit 0
~                                                                                 
~                

##############################################################################################

命令介绍:

chkconfig命令用来更新、查询、修改不同运行级上的系统服务。比如安装了httpd服务,并且把启动的脚本放在了/etc/rc.d/init.d目录下,有时候需要开机自动启动它,而有时候则不需要,因此,就可以使chkconfig命令来进行控制,这个命令就相当于一个开关,不过这个开关有[0-6]共7个档.

# 0 - 停机
# 1 - 单用户模式 
# 2 - 多用户,没有NFS 
# 3 - 完全多用户模式(标准的运行级) 
# 4 - 没有用到 
# 5 - X11(xwindow) 
# 6 - 重新启动 

表示在不同级别下的运行状态是on还是off。所以千万不用讲运行级别设置为0,6;最常用的就是2,3,5

chkconfig --list [name] 服务列表[可根据实际需要,停掉不用服务]
chkconfig --add  [name] 服务添加[如缺省,则从缺省的init脚本自动建立] 
chkconfig --del  [name] 服务删除[并把相关符号连接从/etc/rc[0-6].d删除]
chkconfig --level name <on|off|reset>  
on    服务在改变运行级时的启动
off   服务在改变运行级时的停止
reset 指初始化服务信息
level 指运行级别;比如235表示运行级别为2、3、5,默认新增服务2、3、4、5

 

至于配置文件,可以放置到init的初始文件中,也可以再shell脚本中添加:

 

 
  1. 例1:random.init 包含三行:

  2. # chkconfig: 2345 20 80

  3. # description: Saves and restores system entropy pool for \

  4. # higher quality random number generation.

  5. 表明 random 脚本应该在运行级 2, 3, 4, 5 启动,启动优先权为20,停止优先权为:80

 
  1. 例2: 配置文件写在运行脚本中

  2. [root@linux ~]# cat /etc/init.d/test

  3. #!/bin/bash

  4. # chkconfig: 345 30 70

  5. # description: Test service

  6. # author: Jerry_1126

  7. # version: v1.01

常用例子:

 

 

  • 例子1: 脚本中检查服务的启动;

 

 

# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig httpd   && echo "httpd service is configured"

# ./check.sh
Network service is configured

 

NOTE:chkconfig直接加服务名,如果返回真的话,echo信息。也可检查运行级别

 

# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"

# ./check1.sh
Network service is configured for level 3
  • 例子2: 检查当前运行的服务及级别
# chkconfig --list
abrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:off
acpid   0:off   1:off   2:off   3:off   4:off   5:off   6:off
atd     0:off   1:off   2:off   3:on    4:on    5:on    6:off
...

如果只想查运行级别为3且开关打开的,则可以:

 

 

chkconfig --list | grep 3:on

如果只想查看具体某个服务,则可以:

 

 

chkconfig --list | grep network
  • 例子3: 添加服务,自动会在2,3,4,5打开
# chkconfig --list | grep iptables

# chkconfig --add iptables

# chkconfig --list | grep iptables
iptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 例子4: 删除服务
# chkconfig --list | grep ip6tables
ip6tables       0:off   1:off   2:off   3:on   4:off   5:off   6:off

# chkconfig --del iptables

# chkconfig --list | grep iptables
  • 例子5: 打开、关闭运行级别的服务
# chkconfig --level 5 mysql off        # 在运行级别为5的开关上,关闭mysql服务
# chkconfig --level 235 mysql on       # 在运行级别为2,3,5开关上,打开的mysql服务
  • 例子6: 检查rc.d子脚本下的脚本文件
# chkconfig --list | grep xinetd
xinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:off
xinetd based services:

# cd /etc/rc.d/rc3.d
# ls | grep xinetd
K08xinetd             #关闭的时候,杀掉K开头的文件
S14xinetd             #启动的时候,启动S开头的文件
  • 例子7: 执行添加命令时,rc.d目录下脚本变化

假如nfsserver没启动,那么在/etc/rc.d/rc*.d目录下,不存在文件

 

 

# chkconfig  --list | grep nfsserver
nfsserver                 0:off  1:off  2:off  3:off  4:off  5:off  6:off

# ls /etc/rc.d/rc3.d | grep nfsserver

# ls /etc/rc.d/rc5.d | grep nfsserver

假如nfsserver服务启动后,目录变化:

 

 

# chkconfig --add nfsserver
nfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off

# cd  /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

 

假如nfsserver服务关闭后,目录变化:

 

# chkconfig --level 5 nfsserver off

# ls /etc/rc.d/rc5.d  | grep nfsserver

自定义服务添加:

举个简单例子,说明下一个自定义服务添加的详细过程:

 

 
  1. [root@linux init.d]# cd /etc/init.d/test #进入目录

  2. [root@linux init.d]# touch test #在该目录下,新建个服务脚本

  3. [root@linux init.d]# cat test #脚本内容

  4. #!/bin/bash

  5. # chkconfig: 345 30 70 #此行必须有,运行级别3,4,5,启动时权限30,关闭时权限70

  6. # description: Test Service Running difference level

  7. # author: Jerry_1126

  8. # version: v1.01

  9.  
  10. case "$1" in

  11. stop) echo -e "The Test service is ${1}ed! \n"

  12. ;;

  13. start) echo -e "The Test service is ${1}ed! \n"

  14. ;;

  15. restart) echo -e "The Test service is restart! \n"

  16. ;;

  17. *) echo -e "The parameter is wrong! \n"

  18. ;;

  19. esac

  20. [root@linux init.d]# chmod +x ./test

  21. [root@linux init.d]# chkconfig --add test

  22. [root@linux init.d]# service test start

  23. The Test service is started!

  24.  
  25. [root@linux init.d]# chkconfig --list | grep test

  26. test 0:off 1:off 2:on 3:on 4:on 5:on 6:off

  27. [root@linux init.d]# chkconfig --level 3 test on

  28. [root@linux init.d]# chkconfig --list | grep test

  29. test 0:off 1:off 2:off 3:on 4:off 5:off 6:off

附录:

 

附1:常用服务介绍

 

 
  1. amd: # 自动安装网络文件系统守侯进程

  2. apmd: # 高级电源管理

  3. Arpwatch: # 记录日志并构建一个在LAN接口上看到的以太网地址和IP地址对数据库

  4. Autofs: # 自动安装管理进程automount,与NFS相关,依赖于NIS

  5. Bootparamd: # 引导参数服务器,为LAN上的无盘工作站提供引导所需的相关信息

  6. crond: # 计划任务

  7. Dhcpd: # 启动一个动态IP地址分配服务器

  8. Gated: # 网关路由守候进程,使用动态的OSPF路由选择协议

  9. Httpd: # WEB服务器

  10. Inetd: # 支持多种网络服务的核心守候程序

  11. Innd: # Usenet新闻服务器

  12. Linuxconf: # 允许使用本地WEB服务器作为用户接口来配置机器

  13. Lpd: # 打印服务器

  14. Mars-nwe: # mars-nwe文件和用于Novell的打印服务器

  15. Mcserv: # Midnight命令文件服务器

  16. named: # DNS服务器

  17. netfs: # 安装NFS、Samba和NetWare网络文件系统

  18. network: # 激活已配置网络接口的脚本程序

  19. nfs: # 打开NFS服务

  20. nscd: # nscd服务器,用于NIS一个支持服务,它高速缓存用户口令和组成成员关系

  21. portmap: # RPC portmap管理器,与inetd类似,它管理基于RPC服务的连接

  22. postgresql: # 一种SQL数据库服务器。

  23. routed: # 路由守候进程,使用动态RIP路由选择协议

  24. rstatd: # 一个为LAN上的其它机器收集和提供系统信息的守候程序

  25. ruserd: # 这是一个基于RPC的服务,它提供关于当前记录到LAN上一个机器日志中的用户信息

  26. rwalld: # 这是一项基于RPC的服务,允许用户给每个注册到LAN机器的其他终端写消息

  27. rwhod: # 激活rwhod服务进程,它支持LAN的rwho和ruptime服务

  28. sendmail: # 邮件服务器sendmail

  29. smb: # Samba文件共享/打印服务

  30. snmpd: # 本地简单网络管理候进程

  31. squid: # 激活代理服务器squid

  32. syslog: # 一个让系统引导时起动syslog和klogd系统日志守候进程的脚本

  33. xfs: # X Window字型服务器,为本地和远程X服务器提供字型集

  34. xntpd: # 网络时间服务器

  35. ypbind: # 为NIS(网络信息系统)客户机激活ypbind服务进程

  36. yppasswdd: # NIS口令服务器

  37. ypserv: # NIS主服务器

  38. gpm: # 管鼠标的服务

  39. identd: # AUTH服务,在提供用户信息方面与finger类似

附2:HTTP Service的完整脚本

 

 
  1. #!/bin/bash

  2. #

  3. # httpd Startup script for the Apache HTTP Server

  4. #

  5. # chkconfig: - 85 15

  6. # description: Apache is a World Wide Web server. It is used to serve \

  7. # HTML files and CGI.

  8. # processname: httpd

  9. # config: /etc/httpd/conf/httpd.conf

  10. # config: /etc/sysconfig/httpd

  11. # pidfile: /var/run/httpd.pid

  12.  
  13.  
  14. # Source function library.

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

  16.  
  17.  
  18. if [ -f /etc/sysconfig/httpd ]; then

  19. . /etc/sysconfig/httpd

  20. fi

  21.  
  22.  
  23. # Start httpd in the C locale by default.

  24. HTTPD_LANG=${HTTPD_LANG-"C"}

  25.  
  26.  
  27. # This will prevent initlog from swallowing up a pass-phrase prompt if

  28. # mod_ssl needs a pass-phrase from the user.

  29. INITLOG_ARGS=""

  30.  
  31.  
  32. # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

  33. # with the thread-based "worker" MPM; BE WARNED that some modules may not

  34. # work correctly with a thread-based MPM; notably PHP will refuse to start.

  35.  
  36.  
  37. # Path to the apachectl script, server binary, and short-form for messages.

  38. apachectl=/usr/sbin/apachectl

  39. httpd=${HTTPD-/usr/sbin/httpd}

  40. prog=httpd

  41. pidfile=${PIDFILE-/var/run/httpd.pid}

  42. lockfile=${LOCKFILE-/var/lock/subsys/httpd}

  43. RETVAL=0

  44. STOP_TIMEOUT=${STOP_TIMEOUT-10}

  45.  
  46.  
  47. # check for 1.3 configuration

  48. check13 () {

  49. CONFFILE=/etc/httpd/conf/httpd.conf

  50. GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"

  51. GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"

  52. GONE="${GONE}AccessConfig|ResourceConfig)"

  53. if LANG=C grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then

  54. echo

  55. echo 1>&2 " Apache 1.3 configuration directives found"

  56. echo 1>&2 " please read /usr/share/doc/httpd-2.2.3/migration.html"

  57. failure "Apache 1.3 config directives test"

  58. echo

  59. exit 1

  60. fi

  61. }

  62.  
  63.  
  64. # The semantics of these two functions differ from the way apachectl does

  65. # things -- attempting to start while running is a failure, and shutdown

  66. # when not running is also a failure. So we just do it the way init scripts

  67. # are expected to behave here.

  68. start() {

  69. echo -n $"Starting $prog: "

  70. check13 || exit 1

  71. LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

  72. RETVAL=$?

  73. echo

  74. [ $RETVAL = 0 ] && touch ${lockfile}

  75. return $RETVAL

  76. }

  77.  
  78.  
  79. # When stopping httpd a delay (of default 10 second) is required

  80. # before SIGKILLing the httpd parent; this gives enough time for the

  81. # httpd parent to SIGKILL any errant children.

  82. stop() {

  83. echo -n $"Stopping $prog: "

  84. killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd

  85. RETVAL=$?

  86. echo

  87. [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

  88. }

  89. reload() {

  90. echo -n $"Reloading $prog: "

  91. if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

  92. RETVAL=6

  93. echo $"not reloading due to configuration syntax error"

  94. failure $"not reloading $httpd due to configuration syntax error"

  95. else

  96. # Force LSB behaviour from killproc

  97. LSB=1 killproc -p ${pidfile} $httpd -HUP

  98. RETVAL=$?

  99. if [ $RETVAL -eq 7 ]; then

  100. failure $"httpd shutdown"

  101. fi

  102. fi

  103. echo

  104. }

  105.  
  106.  
  107. # See how we were called.

  108. case "$1" in

  109. start)

  110. start

  111. ;;

  112. stop)

  113. stop

  114. ;;

  115. status)

  116. status -p ${pidfile} $httpd

  117. RETVAL=$?

  118. ;;

  119. restart)

  120. stop

  121. start

  122. ;;

  123. condrestart|try-restart)

  124. if status -p ${pidfile} $httpd >&/dev/null; then

  125. stop

  126. start

  127. fi

  128. ;;

  129. force-reload|reload)

  130. reload

  131. ;;

  132. graceful|help|configtest|fullstatus)

  133. $apachectl $@

  134. RETVAL=$?

  135. ;;

  136. *)

  137. echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|hel

  138. p|configtest}"

  139. RETVAL=2

  140. esac

 

 

chkconfig的使用以及http脚本编写为转载 路径:https://blog.csdn.net/jerry_1126/article/details/38684201

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值