Linux操作系统的服务管理笔记

来源:互联网 发布:io域名备案 编辑:程序博客网 时间:2024/05/18 01:24

服务(service)

服务是指以守侯进程(daemon)方式常驻内存提供功能的程序。

如httpd服务,mysqld服务
服务的命名规范 通常在服务的名称之后会加上一个 d ,d 代表的就是 daemon 的意思

服务分类

按启动方式分类

1.stand alone 可以自行启动的服务。
2.super daemon 这一种服务的启动方式则是藉由一个统一的 daemon 来负责唤起服务,这个特殊的 daemon 就被称为 super daemon 。 早期的 super daemon 是 inetd 这一个,后来则被 xinetd 所取代了。

按工作形态分类

1.signal-control 这种 daemon 是透过讯号来管理的,只要有任何客户端的需求进来,他就会立即启动去处理!例如打印机的服务 (cupsd)。
2.interval-control 这种 daemon 则主要是『每隔一段时间就主动的去运行某项工作』,所以,你要作的是在配置文件指定服务要进行的时间与工作, 该服务在指定的时间才会去完成工作。我们在第十六章提到的 atd 与 crond 就属于这种类型的 daemon 啦 (每分钟侦测一次配置文件)

init 系统

Linux 系统的 init 进程经历了两次重大的演进,传统的 sysvinit 已经淡出历史舞台,新系统 UpStart 和 systemd 各有特点,而越来越多的 Linux 发行版采纳了 systemd

SysV init

结构

/etc/init.d/* :启动脚本放置处
/etc/sysconfig/* :各服务的初始化环境配置文件
/etc/xinetd.conf, /etc/xinetd.d/* :super daemon 配置文件
/var/lib/* :各服务产生的数据库
/var/run/* :各服务的程序之 PID 记录处
服务脚本示例:

#!/bin/sh# For RedHat and cousins:# chkconfig: - 99 01# description: NTP daemon# processname: /usr/sbin/ntp### BEGIN INIT INFO# Provides:          /usr/sbin/ntp# Required-Start:# Required-Stop:# Default-Start:     2 3 4 5# Default-Stop:      0 1 6# Short-Description: NTP daemon# Description:       NTP daemon### END INIT INFOcmd="/usr/sbin/ntp "-D""name=$(basename $0)pid_file="/var/run/$name.pid"stdout_log="/var/log/$name.log"stderr_log="/var/log/$name.err"get_pid() {    cat "$pid_file"}is_running() {    [ -f "$pid_file" ] && /usr/sbin/ntp -Pid $(get_pid) > /dev/null 2>&1}case "$1" in    start)        if is_running; then            echo "Already started"        else            echo "Starting $name"            $cmd >> "$stdout_log" 2>> "$stderr_log" &            echo $! > "$pid_file"            if ! is_running; then                echo "Unable to start, see $stdout_log and $stderr_log"                exit 1            fi        fi    ;;    stop)        if is_running; then            echo -n "Stopping $name.."            kill $(get_pid)            for i in {1..10}            do                if ! is_running; then                    break                fi                echo -n "."                sleep 1            done            echo            if is_running; then                echo "Not stopped; may still be shutting down or shutdown may have failed"                exit 1            else                echo "Stopped"                if [ -f "$pid_file" ]; then                    rm "$pid_file"                fi            fi        else            echo "Not running"        fi    ;;    restart)        $0 stop        if is_running; then            echo "Unable to stop, will not attempt to start"            exit 1        fi        $0 start    ;;    status)        if is_running; then            echo "Running"        else            echo "Stopped"            exit 1        fi    ;;    *)    echo "Usage: $0 {start|stop|restart|status}"    exit 1    ;;esacexit 0

Upstart

采用事件驱动模型,当新硬件被发现时动态启动服务,硬件被拔除时动态停止服务,动态处理系统中的硬件插拔。服务不必在系统启动时都启动,从而节省系统启动时间

Systemd

可以在runtime(运行时)更加简单的监管和控制守护进程,并且简化了监控的实现方式

这里写图片描述

ch​​kconfig

由ch​​kconfig管理的每个服务init.d脚本至少需两行注释
第一行告诉ch​​kconfig 启动级别,启动优先值 停止优先值

可以在任何运行级别中启动,应该使用 - 代替
第二行包含服务的描述

https://www.ibm.com/developerworks/cn/linux/1407_liuming_init2/

参考:
Systemd 入门教程