|NO.Z.00007|——————————|ManageMent|——|Linux&服务管理.V03|

一、RPM 包默认安装的服务管理
### --- 独立服务管理
### --- 独立服务的启动管理

~~~     使用/etc/init.d/目录中的启动脚本启动服务
[root@localhost ~]# /etc/init.d/httpd start
~~~     使用 service 命令来启动独立的服务
[root@localhost ~]# service 独立服务名 start|stop|restart|…
二、独立服务的自启动管理
### --- 使用 chkconfig 服务自启动管理命令
~~~     选项:
~~~     --level:    设定在哪个运行级别中开机自启动(on),或是关闭自启动(off)

[root@localhost ~]# chkconfig [--level 运行级别] [独立服务名] [on|off]
[root@server21 ~]# chkconfig --list | grep httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@server21 ~]# chkconfig httpd on                               // 开启开机自启动
[root@server21 ~]# chkconfig --list | grep httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@server21 ~]# chkconfig httpd off                              // 关闭开机自启动
### --- 修改/etc/rc.d/rc.local 文件,设置服务自启动
~~~     若是让两个开机自启动都生效,会报错。开机会自启动2次,建议配置一个。

[root@localhost ~]# vim /etc/rc.d/rc.local                          // 推荐使用此方法设置开机自启动
touch /var/lock/subsys/local
/etc/rc.d/init.d/httpd  start
### --- 使用 ntsysv 命令管理自启动
~~~     选项:
~~~     --level 运行级别:可以指定设定自启动的运行级别,这个命令的操作是这样的:
~~~     上下键:在不同服务之间移动
~~~     空格键:选定或取消服务的自启动。就是在服务之前是否打入“*”
~~~     tab 键:在不同项目间切换
~~~     F1 键:显示服务的说明
 
[root@localhost ~]# ntsysv [--level 运行级别]
[root@server21 ~]# chkconfig --list |grep httpd                     // 可以通过chkconfig查看
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

一、基于 xinetd 服务的管理
### --- 基于 xinetd 服务的启动

~~~     我们使用 telnet 服务来举例,telnet 服务是用来进程系统远程管理的,端口时 23。
~~~     不过需要注意的是 telnet 的远程管理数据在网络当中是明文传输,非常不安全。
~~~     所以我们在生产服务器上是不建议启动 telnet 服务的,我们这里只是举例而已。
~~~     在生成服务器上,远程管理使用的是 ssh 协议,ssh 是加密的更加安全。
### --- 在生产环境中坚决不可以安装telnet;
### --- 安装Telnet的服务端

[root@server21 ~]# rpm -ivh /mnt/cdrom/Packages/telnet-server-0.17-48.el6.x86_64.rpm
### --- chkconfig查看是基于xinetd服务

[root@server21 ~]# chkconfig --list
    telnet:         off
[root@server21 ~]# vim /etc/xinetd.d/telnet 
# default: on
# description: The telnet server serves telnet sessions; it uses \
#       unencrypted username/password pairs for authentication.service telnet                                               // 服务的名称为 telnet
service telnet
{
flags           = REUSE                                         // 标志为 REUSE,设定 TCP/IP socket 可重用
socket_type     = stream                                        // 使用 TCP 协议数据包
wait            = no                                            // 允许多个连接同时连接
user            = root                                          // 启动服务的用户为 root
server          = /usr/sbin/in.telnetd                          // 服务的启动程序
log_on_failure  += USERID                                       // 登陆失败后,记录用户的 ID
disable         = yes                                           // 服务不启动
}
[root@server21 ~]# vim /etc/xinetd.d/telnet 
~~~     修改配置文件
service telnet
{
~~~     …省略部分输出…
disable             = no                                        // 把 yes 改为 no 
}
### --- 启动telnet,只能启动xinetd服务。

[root@server21 ~]# service xinetd restart
[root@server21 ~]# chkconfig --list 
    telnet:         on  
二、基于 xientd 服务的自启动
### --- 基于 xientd 服务的自启动
~~~     使用 chkconfig 命令管理自启动
~~~     使用 ntsysv 命令管理自启动
~~~     基于 xinetd 的服务,没有自己的运行级别,是依靠 xinetd 服务的运行级别。
~~~     所以不用指定--level 选项

[root@localhost ~]# chkconfig 服务名 on|off
三、独立服务的启动脚本分析
### --- 独立服务的启动脚本分析

~~~     既然独立的服务启动是依靠/etc/init.d/httpd 这个脚本来进行启动管理的,
~~~     那么这个脚本中到底是什么样子的?既然我们已经学习了 shell 脚本,
~~~     那么我们就来学习一下这个脚本到底是怎么实现apache 服务的管理的。
#!/bin/bash
# 
# httpd
# Startup script for the Apache HTTP Server
# 
#  chkconfig: - 85 15                                   // 下列两句话不是注释,而是chkconfig可以被调用
#  description: The Apache HTTP Server is an efficient and extensible \
# 自启动设定 -代表自启动级别,85(S85)代表启动序号,15(K15)代表关闭序号。
# 
#   server implementing the current HTTP standards.
# 服务描述。以上两行用于 apache 自启动。
#  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
# "."其实就是 source,就是调用 functions 文件。
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# 判断 httpd 如果是文件,则调用 httpd 文件。
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# 定义变量 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
}
#  定义 start 函数,用于 apache 的启动。
#  如 果 守 护 进 程 /usr/sbin/httpd 启 动 成 功 ( $RETVAL = 0 ) , 
#  就 建 立 /var/lock/subsys/httpd 文 件 ( touch${lockfile})。
#  通过$httpd 变量执行/usr/sbin/httpd 命令启动 apache。通过$pidfile 变量调用 apache的 PID。
#  通过变量$OPTIONS 定义命令执行时的初始化环境配置,依赖/etc/sysconfig/httpd 文件。
#  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}
}
# 定义 stop 函数,用来关闭 apache 服务,关闭服务之后会删除 pid 文件。
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
}
# 定义 reload 函数,用于 apache 的重新加载。
# 通过/usr/sbin/httpd –t 命令判断 apache 的配置文件。如果配置文件报错,则输出错误提示。
# 如果配置文件正确,则重新加载 apache。
# See how we were called.
case "$1" in
# 判断执行脚本后的第一个参数的值,$1 表示执行脚本时的第一个参数。
start)
start
;;
;;
# 如果参数值为 start,则调用 start 函数。
stop)
stop
;;
# 如果参数值为 stop,则调用 stop 函数。
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
# 如果参数值为 status,则执行 status –p $httpd 命令测试 apache 状态。
restart)
stop
start
;;
# 如果参数值为 restart,则先调用 stop 函数,再调用 start 函数
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
# 如果参数值为 condrestart 或 try-restart,
# 则只有 apache 服务是已经运行时才先调用 stop 函数,再调用 start 函数,
# 重启 apache。如果 apache 服务没有运行,则不重启 apache。
force-reload|reload)
reload
;;
# 如果参数值为 force-reload 或 reload,则调用 reload 函数。
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
# 如果参数是 graceful 或 help 或 configtest 或 fullstatus,
# 则执行/usr/sbin/apachectl 命令,并把参数作为命令的参数传入 apachectl 命令。
*)
echo
$"Usage:
$prog
{start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|config
test}"
RETVAL=2
# 如果输出的参数不是以上任何参数,则输出错误信息
esac
exit $RETVAL
# 通过这个脚本,我们可以对 apache 服务的启动有更深的了解了。

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示