Linux运维——服务管理

Linux运维——服务管理

一、服务分类

1、RPM包安装的服务

1.1独立的服务

这些服务是通过RPM包安装的,可以被服务管理命令识别。

  • 查看
chkconfig --list
  1. 启动管理

    1. 使用/etc/init.d/目录中的启动脚本启动服务

      1. /etc/init.d/[服务名] start
        
    2. 使用service命令启动服务

      1. service [服务名] start|stop|restart|...
        
  2. 自启动管理

    1. 使用chkconfig服务自启动管理命令

      1. chkconfig [--level 运行级别] [服务名] [on|off]
        --level 设定在那个运行级别中开机自启动
        # 可以省略--level选项默认设置为2345
        
    2. 修改/etc/rc.d/rc.local设置服务自启动

      1. 将上方/etc/init.d/启动方式命令添加到此文件中即可实现开机启动
    3. 使用ntsysv命令实现命令行图形化方式开启

1.2基于xinetd的服务(了解即可)

此服务在新版本中默认不安装,需要手动安装

yum -y install xinetd
chkconfig --list
  1. 服务的启动

    1. 使用telnet服务来举例,telnet服务是用来进程系统远程管理的,端口是23。在telnet的远程管理中数据在网络中是明文传输,非常不安全。所以在生产服务器中是不建议启动telnet服务的。

    2. vim /etc/xinet.d/telnet
      
      将 disable=yes 修改为 disable=no
      # 重启xinetd服务
      service xinetd restart
      
  2. 服务自启动

    1. 使用chkconfig命令管理自启动

      1. chkconfig [服务名] on|off
        
      2. 不可以添加运行级别号

    2. 使用ntsysv命令管理自启动

1.3独立服务的启动脚本源码
vim /etc/init.d/httpd

#!/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
# 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}
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() {
        status -p ${pidfile} $httpd > /dev/null
        if [[ $? = 0 ]]; then
      status -p ${pidfile} $httpd > /dev/null
        if [[ $? = 0 ]]; then
                echo -n $"Stopping $prog: "
                killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
        else
                echo -n $"Stopping $prog: "
                success
        fi
        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
             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
        ;;
        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

2、源码包安装的服务

2.1启动管理
[安装路径]/bin/apachectl start|stop|restart|...
2.2自启动管理

将启动命令添加到/etc/rc.local文件中

2.3让源码包可以被服务管理命令识别

使用service命令控制源码包安装的服务

# 将安装目录的启动脚本软连接到init.d文件夹中
ln -s [apache安装目录]/bin/apachectl /etc/init.d/apache
# 使用服务管理命令
service apache restart

使源码包服务可以被chkconfig命令管理自启动

  • 格式
# chkconfig: [运行级别] [启动顺序] [关闭顺序]
# discription: xxxx
  • 实例
vim /etc/init.d/apache

#!/bin/sh
#
# chkconfig: 35 86 76
#
# discription: source package apache
  • 将服务加入chkconfig
chkconfig --add [服务名]

chkconfig配置完成后ntsysv也可以使用

二、Linux中常见服务的作用

服务名称功能建议
acpid电源管理接口。笔记本建议开启,可以监听内核层的相关电源事件开启
anacron系统的定时任务程序。cron的一个子系统,如果定时任务错过了执行事件,可以通过anacron继续唤醒执行关闭
alsasoundAlsa声卡启动。如果使用alsa声卡则开启关闭
apmd电源管理模块。如果支持acpid,就不需要apmd,可以关闭关闭
atd指定系统在特点事件执行某个任务,只能执行一次。如果需要则开启,但我们一般使用crond来进行循环定时任务关闭
auitd审核子系统,如果开启了此服务,SELinux的审核信息会写入 /var/log/audit/audit.log 文件,如果不开启,审核信息会记录在syslog中开启
autofs让服务器可以自动挂载网络中的其他服务器的共享数据,一般用来自动挂载 NFS 服务。关闭
avahi-daemonAvahi 是 zeroconf 协议的实现。它可以在没有 DNS 服务的局域网中发现基于 zeroconf 协议的设备和服务。关闭
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
自动化运维工具——ansible是一款开源的IT自动化工具,广泛应用于软件部署、配置管理和任务协同等领域。它的最大特点是简单易用、功能强大且跨平台。 首先,ansible具有简单易用的特点。它使用简单的YAML语言作为配置文件,无需编写复杂的脚本。用户只需要简单地定义主机和操作即可完成任务的执行。这使得即便是非开发人员,也能轻松上手使用ansible进行自动化管理。 其次,ansible功能强大。它支持广泛的操作系统、云平台和网络设备,并提供了丰富的模块化功能。通过在任务中使用ansible的模块,我们可以实现系统配置、软件安装、文件传输等各种常见操作。此外,ansible不仅支持并发执行任务,还能够将任务分组执行,并提供了强大的变量和条件控制功能。 再次,ansible跨平台。无论是在Linux、Unix还是Windows系统上,ansible都能够良好地运行。此外,它还支持云平台,如AWS、Azure等,以及网络设备,如Cisco、Juniper等。这使得ansible成为一个非常灵活的自动化运维工具,能够满足各种不同环境和需求的自动化管理。 最后,ansible还具有良好的社区支持。ansible拥有庞大的用户社区和活跃的开发者社区,有大量的文档、示例和插件可供参考和使用。这使得我们在使用ansible时能够获得快速解答和支持,同时也能够从社区中学习到更多的技巧和经验。 总而言之,ansible是一款简单易用、功能强大且跨平台的自动化运维工具。它在软件部署、配置管理和任务协同等方面具有广泛的应用,并且得到了良好的社区支持。无论是企业还是个人,都可以通过ansible来提高工作效率和自动化管理水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值