我是靠谱客的博主 瘦瘦跳跳糖,这篇文章主要介绍ContOS 7 下 Systemctl 的介绍和使用技巧,现在分享给大家,希望可以做个参考。

文章目录

        • 一、systemd 的简单介绍
        • 二、systemctl 的简单介绍
        • 三、systemctl 的使用技巧
        • 四、采用 systemctl 管理自定义服务

一、systemd 的简单介绍

  1. 从 CentOS 7.x 开始使用 systemd 服务来替代 daemon ,原来管理系统启动和管理系统服务的相关命令全部由 systemctl 命令来代替

  2. systemctl 是一个 systemd 工具,主要负责控制 systemd 系统和服务管理器

  3. systemd 系统启动和服务管理守护进程管理器,负责在系统启动或者运行时,激活系统资源,服务器进程和其它进程,已成为大多数发行版的标准配置

  4. systemd 特性:

    特性列表
    1、系统引导时实现服务并行启动
    2、按需启动守护进程
    3、自动化的服务依赖关系管理
    4、同时采用 socket 式和 D-Bus 总线式激活服务
    5、系统状态快照
  5. systemd 核心概念 unit (单元)类型,unit 表示不同类型的 systemd 对象,通过配置文件进行标识和配置

    类型作用扩展名
    service用于定义系统服务.serivce
    target用于模拟实现运行级别.target
    device用于定义内核识别的设备-
    mount定义文件系统挂载点-
    socket用于标识进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动-
    snapshot管理系统快照-
    swap用于标识swap设备-
    automount文件系统的自动挂载点-
    path用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务-
    # 查看系统是否安装了 systemd
    systemctl --version
    
    # 检查 systemd 是否运行
    ps -ef | grep systemd
    
    # 如何查看上标中的类型(以 service 为例)
    systemctl -t service
    

二、systemctl 的简单介绍

  1. systemctl 命令与原来的 service 命令对比

    daemon 命令systemctl 命令说明
    service [服务] startsystemctl start [unit type]启动服务
    service [服务] stopsystemctl stop [unit type]停止服务
    service [服务] restartsystemctl restart [unit type]重启服务
    -systemctl status [unit type]看服务运行情况
    -systemctl systemctl daemon-reload重新加载服务,加载更新后的配置文件

    应用举例:

    # 启动网络服务(其中后缀名称可以省略)
    systemctl start network.service
     
    # 停止网络服务(其中后缀名称可以省略)
    systemctl stop network.service
     
    # 重启网络服务(其中后缀名称可以省略)
    systemctl restart network.service
     
    # 查看网络服务状态(其中后缀名称可以省略)
    systemctl status network.serivce
    

    P.S
    systemctl daemon-reload 并不是所有服务都支持这个参数,比如 network.service

  2. systemctl 命令与原来的 chkconfig 命令对比

    设置开机启动/不启动

    daemon 命令systemctl 命令说明
    chkconfig [服务] onsystemctl enable [unit type]设置服务开机启动
    chkconfig [服务] offsystemctl disable [unit type]设备服务禁止开机启动

    应用举例:

    # 停止 mysql 服务(其中后缀名称可以省略)
    systemctl stop mysqld.service
     
    # 禁止 mysql 服务开机启动(其中后缀名称可以省略)
    systemctl disable mysqd.service
     
    # 查看 mysql 服务状态(其中后缀名称可以省略)
    systemctl status mysqld.service
     
    # 重新设置 mysql 服务开机启动(其中后缀名称可以省略)
    systemctl enable mysqld.service
    

    查看系统上所有的服务

    命令格式:  
    systemctl [command] [–type=TYPE] [–all]
    
    # 参数详解:  
    command list-units              依据 unit 列出所有启动的 unit,加上 –all 才会列出没启动的 unit。 
    command list-unit-files         依据 /usr/lib/systemd/system/ 内的启动文件,列出启动文件列表
    command list-units -type=TYPE   TYPE为unit type, 主要有service, socket, target
    

    应用举例:

    systemctl 命令说明
    systemctl列出所有的系统服务
    systemctl list-units列出所有启动 unit
    systemctl list-unit-files列出所有启动文件
    systemctl list-units –type=service –all列出所有 service 类型的 unit
    systemctl list-units –type=service –all grep cpu列出 cpu 电源管理机制的服务
    systemctl list-units –type=target –all列出所有 target
  3. systemctl 命令与 init 命令对比

    init 命令systemctl 命令说明
    init 0systemctl poweroff系统关机
    init 6systemctl reboot重新启动
    -systemctl suspend进入睡眠模式
    -systemctl hibernate进入休眠模式
    -systemctl rescue强制进入救援模式
    -systemctl emergency强制进入紧急救援模式
  4. 设置系统运行级别

    运行级别对照表

    init 级别systemctl target
    0shutdown.target
    1emergency.target
    2rescure.target
    3multi-user.target
    4-
    5graphical.target
    6-

    设置运行级别

    命令格式: 
    systemctl [command] [unit.target]
    
    参数详解:
    [command]:
    get-default:取得当前的target
    set-default:设置指定的target为默认的运行级别
    isolate:    切换到指定的运行级别
    unit.target:为 5.1 表中列出的运行级别
    
    systemctl 命令说明
    systemctl get-default获得当前的运行级别
    systemctl set-default multi-user.target设置默认的运行级别为 mulit-user
    systemctl isolate multi-user.target在不重启的情况下,切换到运行级别 mulit-user 下
    systemctl isolate graphical.target在不重启的情况下,切换到图形界面下
  5. systemctl 特殊的用法

    systemctl 命令说明
    systemctl is-active [unit type]查看服务是否运行
    systemctl is-enabled [unit type]查看服务是否设置为开机启动
    systemctl mask [unit type]注销指定服务
    systemctl unmask [unit type]取消注销指定服务

    应用举例:

    # 查看网络服务是否启动(其中后缀名称可以省略)
    systemctl is-active network.service
     
    # 检查网络服务是否设置为开机启动(其中后缀名称可以省略)
    systemctl is-enable network.service
     
    # 停止 cups 服务(其中后缀名称可以省略)
    systemctl stop cups.service
     
    # 注销 cups 服务(其中后缀名称可以省略)
    systemctl mask cups.service
     
    # 查看 cups 服务状态(其中后缀名称可以省略)
    systemctl status cups.service
     
    # 取消注销 cups 服务(其中后缀名称可以省略)
    systemctl unmask cups.service
    

三、systemctl 的使用技巧

  1. 使用 systemctl 分析各服务之前的依赖关系

    # –reverse 是用来检查哪个 unit 使用了这个 unit
    systemctl list-dependencies [unit] [–reverse]
    

    应用举例:

    # 获得当前运行级别的 target
    [root@www ~]# systemctl get-default
    multi-user.target
     
    # 查看当前运行级别 target(mult-user) 启动了哪些服务
    [root@www ~]# systemctl list-dependencies
    default.target
    ├─abrt-ccpp.service
    ├─abrt-oops.service
    ├─vsftpd.service
    ├─basic.target
    │ ├─alsa-restore.service
    │ ├─alsa-state.service
    .....(中间省略).....
    │ ├─sockets.target
    │ │ ├─avahi-daemon.socket
    │ │ ├─dbus.socket
    .....(中间省略).....
    │ ├─sysinit.target
    │ │ ├─dev-hugepages.mount
    │ │ ├─dev-mqueue.mount
    .....(中间省略).....
    │ └─timers.target
    │   └─systemd-tmpfiles-clean.timer
    ├─getty.target
    │ └─getty@tty1.service
    └─remote-fs.target
     
    # 查看哪些 target 引用了当前运行级别的target
    [root@www ~]# systemctl list-dependencies --reverse
    default.target
    └─graphical.target
    
  2. 关闭网络服务

    在使用 systemctl 关闭网络服务时有一些特殊需要同时关闭 unit.servce 和 unit.socket

    使用 systemctl 查看开启的 sshd 服务

    [root@www system]#  systemctl list-units --all | grep sshd
    sshd-keygen.service loaded inactive dead        OpenSSH Server Key Generation
    sshd.service        loaded active   running     OpenSSH server daemon
    sshd.socket         loaded inactive dead        OpenSSH Server Socket
    

    可以看到系统同时开启了 sshd.service 和 sshd.socket ,如果只闭关了sshd.service 那么 sshd.socket 还在监听网络,在网络上有要求连接 sshd 时就会启动 sshd.service 。因此如果想完全关闭 sshd 服务的话,需要同时停用 sshd.service 和 sshd.socket。

    systemctl stop sshd.service
    systemctl stop sshd.socket
    systemctl disable sshd.service sshd.socket
    

    P.S
    由于 Centos 7.x 默认没有安装 net-tools ,因此无法使用 netstat 来查看主机开发的商品。需要通过 yum 安装来获得该工具包:

    # 安装 net-tools 工具
    yum -y install net-tools
    # 查看是否关闭22端口
    netstat -lnp |grep sshd
    
  3. 关闭防火墙 firewall

    Centos 7.x 中取消了 iptables , 用 firewall 取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:

    systemctl stop firewalld.service
    systemctl disable firewalld.service    
    

四、采用 systemctl 管理自定义服务

  1. systemctl 脚本存放的位置

    系统服务:开机不需要登录就能运行的程序(相当于开机自动启动)

    /usr/lib/systemd/system
    

    用户服务:需要登录后才能运行的程序

    /usr/lib/systemd/user
    
  2. 初窥 MySQL 的 service unit 文件格式

    # Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License, version 2.0,
    # as published by the Free Software Foundation.
    #
    # This program is also distributed with certain software (including
    # but not limited to OpenSSL) that is licensed under separate terms,
    # as designated in a particular file or component or in included license
    # documentation.  The authors of MySQL hereby grant you an additional
    # permission to link the program and your derivative works with the
    # separately licensed software that they have included with MySQL.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License, version 2.0, for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
    #
    # systemd service file for MySQL forking server
    #
    
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    
    [Install]
    WantedBy=multi-user.target
    
    [Service]
    User=mysql
    Group=mysql
    
    Type=notify
    
    # Disable service start and stop timeout logic of systemd for mysqld service.
    TimeoutSec=0
    
    # Execute pre and post scripts as root
    PermissionsStartOnly=true
    
    # Needed to create system tables
    ExecStartPre=/usr/bin/mysqld_pre_systemd
    
    # Start main service
    ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS
    
    # Use this to switch malloc implementation
    EnvironmentFile=-/etc/sysconfig/mysql
    
    # Sets open_files_limit
    LimitNOFILE = 10000
    
    Restart=on-failure
    
    RestartPreventExitStatus=1
    
    # Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
    Environment=MYSQLD_PARENT_PID=1
    
    PrivateTmp=false    
    
  3. service unit 文件 [Unit]、[Service]、[Install] 三部分详解

    • [Unit] 字段主要是服务说明

      字段名说明
      Description服务描述信息
      After定义 unit 的启动秩序,表示当前 unit 应该晚于哪些 unit 启动
      Before定义 unit 的启动秩序,表示当前 unit 应该早于哪些 unit 启动
      Wants依赖到的其它 units,弱依赖
      Conflicts定义 units 间的冲突关系
    • [Service] 字段核心区域

      字段名说明
      Type定义影响 ExecStart 及相关参数的功能的 unit 进程启动类型,Type=forking 表示后台运行模式
      User设置服务运行的用户 User=root
      Group设置服务运行的组 Group=root
      KillMode定义 systemd 如何停止服务 KillMode=control-group
      PIDFile定义 PID 存放的绝对路径 PIDFile=/opt/data/test/test.pid
      Restart定义服务进程退出后,systemd 的重启方式,默认不重启 Restart=no
      RemainAfterExit当该服务的所有进程全部退出之后,是否依然将此服务视为活动(active)状态, 默认值为 no
      PrivateTmp表示给服务分配独立的临时空间 PrivateTmp=true
      ExecStartPre在启动服务之前执行的命令
      ExecStart在启动该服务时需要执行的命令(命令+参数)
      ExecStartPost在启动服务之后执行的命令
      ExecStop这是一个可选的指令, 用于设置当该服务被要求停止时所执行的命令行,语法规则与 ExecStart= 完全相同
      ExecStopPost在停止服务之后执行的命令
      ExecReload重启服务时执行的命令

      P.S
      [Service] 部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!

    • [Install] 字段定义由 systemctl enable 以及 systemctl disable 命令在实现服务启用或禁用时用到的一些选项

      字段名说明
      WantedBy多用户 WantedBy=multi-user.target
    • 部分字段详细说明

      Type参数值含义
      Type=simple以Execstart字段启动的进程为主进程(默认)
      Type=forkingExecstart 字段以 fox() 方式启动,此时父进程将推出,子进程将成为主进程(后台运行),一般都设置为 forking
      Type=oneshot类似于 simple,但只执行一次,systemd 会等他执行完,才执行其他服务
      Type=dbus类似于 simple。但会等待 D-Bus 信号后启动
      Type=notify类似于 simple,但结束后会发出通知信号,然后systemd才启动其他服务
      Type=idle类似于 simple,弹药等到其他任务都执行完,才启动该服务
      Killmode参数值含义
      KillMode=contorl-group当前控制组里所有的子进程都会被杀掉(默认)
      KillMode=process只杀主进程
      KillMode=mixed主进程将收到 SIGTERM(终止进程)信号,子进程将受到 SIGKILL(无条件终止)信号
      KillMode=none没有进程会被杀掉,只是执行服务的stop命令
      Restart参数值含义
      Restart=no默认值,无操作
      Restart=on-success只有正常退出时(退出状态码为0),才会重启
      Restart=on-failure非正常退出时、重启、包括信号终止和超时(对于守护进程,推荐使用该参数)
      Restart=on-abnaomal只有信号终止或超时,才会重启
      Restart=on-abort只有在收到没有捕捉到信号终止时,才会重启
      Restart=on-watchdog超时退出时,才会重启
      Restart=always不管什么退出原因,都会重启
      RestartSec参数值含义
      RestartSec=1设置在重启服务(Restart=)前暂停多长时间。 默认值是100毫秒(100ms)。 如果未指定时间单位,那么将视为以秒为单位。 例如设为"20"等价于设为"20s"。
      WantedBy参数值含义
      WantedBy=multi-user.target表示多用户命令行状态,这个设置很重要
      WantedBy=graphical.target表示图形用户状体,它依赖于 multi-user.target

      更多关于 systemd.service 相关的命令说明,请参考手册《systemd.service 中文手册》

  4. 采用 systemctl 自定义 Nginx 服务

    1. 创建 nginx.service 文件

      # 进入 systemd 文件夹
      cd /usr/lib/systemd/system
      
      # 创建 nginx.service 文件
      vim nginx.service
      
    2. 配置 nginx.service 文件

      [Unit]
      Description=nginx - high performance web server
      After=network.target remote-fs.target nss-lookup.target
      
      [Service]
      Type=forking
      ExecStart=/usr/local/nginx/sbin/nginx
      ExecReload=/usr/local/nginx/sbin/nginx -s reload
      ExecStop=/usr/local/nginx/sbin/nginx -s stop
      
      [Install]
      WantedBy=multi-user.target
      
    3. 采用 systemclt 管理 nginx.service

      systemctl enable|disable|status|start|restart|stop nginx.service
      

最后

以上就是瘦瘦跳跳糖最近收集整理的关于ContOS 7 下 Systemctl 的介绍和使用技巧的全部内容,更多相关ContOS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(237)

评论列表共有 0 条评论

立即
投稿
返回
顶部