• centos7的systemd命令对比


    centos7的systemd命令对比

    http://www.linuxidc.com/Linux/2014-09/106490p2.htm
    http://www.linuxidc.com/Linux/2016-09/135464p2.htm
    http://www.linuxidc.com/Linux/2014-09/106455.htm

    http://www.linuxidc.com/Linux/2015-04/115937.htm


    systemd的核心概念:unit
    unit由其相关的配置文件进行标识、识别和配置,也就是说一个unit到底定义与否,由其配置文件进行标识。这类配置文件中主要包含了几个类别:系统服务,监听的socket、保存的快照以及其他与init相关的信息,这些配置文件中主要保存在:
    /usr/lib/systemd/system/    每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/  
    /run/systemd/system/    系统执行过程中所产生的服务脚本,比上面目录优先运行
    /etc/systemd/system/    管理员建立的执行脚本,类似于/etc/rc.d/rcN.d/Sxx类的功能,比上面目录优先运行

    /etc/rc.d/rc3.d/ |grep mysql
    lrwxrwxrwx  1 root root 16 Dec 22  2016 /etc/rc.d/rc3.d/K36mysqld -> /etc/init.d/mysqld


    关系

    /etc/systemd/system/K36mysqld -> /usr/lib/systemd/system/mysqld



    unit的常见类型:
    service unit:这类unit的文件扩展名为.service,主要用于定义系统服务(其扮演了在centos6上/etc/init.d/目录下的服务脚本的作用)
    target unit:这类unit的文件扩展名为.target,主要用于模拟实现"运行级别"的概念
    device unit:这类unit文件扩展名为.device,用于定义内核识别的设备,然后udev利用systemd识别的硬件,完成创建设备文件名
    mount unit:这类unit文件扩展名为.mount,主要用于定义文件系统挂载点
    socket unit:这类unit文件扩展名为.socket,用于标识进程间通信用到的socket文件
    snapshot unit:这类unit文件扩展名为.snapshot,主要用于实现管理系统快照
    swap unit:这类unit文件扩展名为.swap,主要用于标识管理swap设备
    automount unit:这类unit文件扩展名为.automount,主要用于文件系统自动挂载设备
    path unit:这类unit文件扩展名为.path,主要用于定义文件系统中的文件或目录



    systemd对target类unit的管理(类似于运行级别的管理)
    unit配置文件:.target
    运行级别的对应关系:
    0 --> runlevel0.target,poweroff.target
    1 --> runlevel1.target,rescue.target
    2 --> runlevel2.target,multi-user.target
    3 --> runlevel3.target,multi-user.target
    4 --> runlevel4.target,multi-user.target
    5 --> runlevel5.target,graphical.target
    6 --> runlevel6.target,reboot.target



    systemd如何管理系统上的各个服务
    systemd对服务的管理(service类unit的管理)
    主要是依靠service类型的unit文件进行管控的完成的
    systemctl命令:
    语法:systemctl [OPTIONS…] COMMAND [SERVICE_NAME.service…]
    centos7里面状态信息里显示的loaded 表示是否加入到systemctl可管理的列表中(类似centos6中chkconfig –add的概念,如果是loaded,表示已经添加到列表中,后面disabled表示没有开机自动启动)



    查看所有的服务,也就是所有已经装载load的服务,包含已激活和未激活的:
    chkconfig –-list ==> systemctl list-units -t service -a
    显示出来的信息的含义:
    loaded:Unit配置文件已处理
    active(running):一次或多次持续处理的运行
    active(exited):成功完成一次性的配置
    active(waiting):运行中,等待一个事件
    inactive:不在运行
    enabled:开机启动
    disabled:开机不启动
    static:开机不启动,但可被另一个启用的服务激活


    centos7-》centos6
    服务启动

    systemctl start servicename.service  -> service  servicename start

    服务停止

    systemctl stop servicename.service  -> service  servicename stop

    服务重启

    systemctl restart servicename.service  -> service  servicename restart

    服务状态

    systemctl status servicename.service  -> service  servicename status

    服务重载

    systemctl reload-or-restart servicename.service  -> /bin/kill -HUP  servicename

    服务详细信息,只能看到enabled的服务,不完全等价于 chkconfig  --list

    systemctl list-units -t service  -a  |grep servicename  -> chkconfig --list |grep servicename

    设置开机启动/禁用

    systemctl enable/disable servicename.service  -> chkconfig  servicename on/off

    查看服务依赖关系

    systemctl list-dependencies servicename.service -> 无

    查看禁用的服务

    systemctl is-enabled  servicename.service

    添加服务,不完全等价于chkconfig --add

    systemctl daemon-reload    -> chkconfig --add  servicename


    运行级别切换

    systemctl isolate name.target -> init 运行级别对应数字

    查看当前运行级别

    systemctl list-units -t target  -> runlevel

    查看所有loaded的target

    systemctl list-units -t target  -a -> 无

    查看默认运行级别

    systemctl get-default  -> 查看/etc/inittab文件中的定义

    设定默认运行级别

    systemctl set-default  name.target -> 修改/etc/inittab文件中的定义

    系统引导时间分析

    systemd-analyze blame















    centos7新增的紧急模式和救援模式

    centos7启动项变化
    系统启动时,通过修改grub选项中的相关内核参数,实现启动时进入不同的模式(紧急救援模式、emergency模式)
    在启动时,启动菜单项按e键,然后在linux16行后添加:(设置内核参数,只影响当次启动)
    按ctrl-x启动


    systemd版本
    cat /etc/redhat-release
    CentOS Linux release 7.3.1611 (Core)

    systemctl --version
    systemd 219
    +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN


    f

    f


    service unit file文件的组成

    如利用systemctl set-default runlevel5.target 修改默认运行级别后,原来/etc/systemd/system/default.target会重新链接到/usr/lib/systemd/system/graphical.target

    service unit file文件的组成:
    文件通常由三部分组成:
    [Unit]:定义与Unit类型无关的通用选项,用于提供unit的描述信息,unit行为及依赖关系等;
    [Service]:与特定类型相关的专用选项,此处为service类型
    [Install]:定义由“systemctl enable”以及“systemctl disable”命令在实现服务启用或仅用时用到的一些选项;

    unit段的常用选项:
    Description:描述信息,意义性描述
    After:定义unit启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
    Requies:依赖到的其他units;强依赖,被依赖的unit无法激活时,当前unit也无法激活
    Wants:指明依赖到的其他units;弱依赖,被依赖的unit无法激活时,当前unit可以被激活
    Conflicts:定义units间的冲突关系


    service段的常用选项:
    Type:用于定义ExecStart及相关参数的功能的unit进程启动类型;
    类型:
    simple:默认值,表示由ExecStart启动的进程为主进程
    forking:表示由ExecStart启动的进程生成的其中一个子进程将成为主进程,启动完成后,父进程会退出
    oneshot:功能类似于simple,但是在启动后续的units进程之前,主进程将会退出
    notify:类似于simple,表示后续的units,仅在通过sdnotify函数发送通知以后,才能运行该命令

    idle:
    常见的是notify、forking、simple
    EnvironmentFile :指明环境配置文件,为真正ExecStart执行之前提供环境配置或变量定义等文件
    ExecStart:指明启动unit要运行的命令或脚本;ExecStartPre、ExecStartPost表示启动前或启动后要执行的命令或脚本
    ExecStop:指明停止unit要运行的命令或脚本
    Restart:表示进程意外终止了,会自动重启

    install段的常用选项:
    Alias:当前unit的别名
    RequiredBy:被那些units所依赖,强依赖
    WantedBy:被那些units所依赖,弱依赖

    cat /usr/lib/systemd/system/sshd.service 
    [Unit]
    Description=OpenSSH server daemon
    Documentation=man:sshd(8) man:sshd_config(5)
    After=network.target sshd-keygen.service
    Wants=sshd-keygen.service
    
    [Service]
    Type=forking
    PIDFile=/var/run/sshd.pid
    EnvironmentFile=/etc/sysconfig/sshd
    ExecStart=/usr/sbin/sshd $OPTIONS
    ExecReload=/bin/kill -HUP $MAINPID
    KillMode=process
    Restart=on-failure
    RestartSec=42s
    
    [Install]
    WantedBy=multi-user.target

    添加服务步骤

    xx.service 模版
    vim /usr/lib/systemd/system/xx.service   #改

    [Unit]
    Description=xx Server #改
    After=network.target
    After=syslog.target
    
    [Install]
    WantedBy=multi-user.target
    
    [Service]
    Type=forking
    #PIDFile=/var/run/mysqld.pid
    ExecStart=/usr/local/mysql/support-files/mysql.server start       #改 /etc/init.d/nginx
    ExecStop=/usr/local/mysql/support-files/mysql.server stop     #改
    PrivateTmp=true

    第一步:

    vim /usr/lib/systemd/system/mysqld.service   

    [Unit]
    Description=MySQL Server  
    After=network.target
    After=syslog.target
    
    [Install]
    WantedBy=multi-user.target
    
    [Service]
    Type=forking
    #PIDFile=/var/run/mysqld.pid
    ExecStart=/usr/local/mysql/support-files/mysql.server start      
    ExecStop=/usr/local/mysql/support-files/mysql.server stop     
    PrivateTmp=true

    第二步注册

    systemctl daemon-reload

    第三步查看是否注册成功

    systemctl is-enabled  mysqld.service

    第四步设置开机启动

    systemctl enable mysqld.service

    第五步启动服务

    systemctl start mysqld.service

    ------------------------------------------------------------

    ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
    ExecStop=/usr/local/bin/redis-cli shutdown

    或者自己写一个脚本启动停止redis也可以
    ExecStart=/tmp/redisinstall.sh  start
    ExecStop=/tmp/redisinstall.sh  stop


    记录 systemd 的日志
    journald是systemd独有的日志系统,替换了sysV init中的syslog守护进程。命令journalctl用来读取日志。

    查看引导信息
    journalctl -b命令来查看所有引导日志。
    journalctl -b


    即时显示引导日志
    实时显示系统日志(类似tail -f)。
    journalctl -f

    查看特定服务的日志
    运用journalctl来查看你只想看到的服务或可执行程序的日志。
    journalctl /usr/sbin/dnsmasq

    f

  • 相关阅读:
    Jest | expect常用匹配方法
    typescript | typeScript 笔记
    好用的思维脑图
    VSCode launch.json 配置详解
    各种语法技术栈文档汇总|api文档集合
    浏览器的运行机制
    js字符串转数字长度限制|超过长度有误差
    css对话框| 气泡框| css箭头
    nginx和php-fpm的用户权限
    mac安装redis拓展
  • 原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/8351825.html
Copyright © 2020-2023  润新知