文章目录
- 一、systemd 的简单介绍
- 二、systemctl 的简单介绍
- 三、systemctl 的使用技巧
- 四、采用 systemctl 管理自定义服务
一、systemd 的简单介绍
-
从
CentOS 7.x开始使用systemd服务来替代daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替 -
systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器 -
systemd系统启动和服务管理守护进程管理器,负责在系统启动或者运行时,激活系统资源,服务器进程和其它进程,已成为大多数发行版的标准配置 -
systemd特性:特性列表 1、系统引导时实现服务并行启动 2、按需启动守护进程 3、自动化的服务依赖关系管理 4、同时采用 socket式和D-Bus总线式激活服务5、系统状态快照 -
systemd核心概念unit(单元)类型,unit表示不同类型的systemd对象,通过配置文件进行标识和配置类型 作用 扩展名 service 用于定义系统服务 .serivcetarget 用于模拟实现运行级别 .targetdevice 用于定义内核识别的设备 - mount 定义文件系统挂载点 - socket 用于标识进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动 - snapshot 管理系统快照 - swap 用于标识swap设备 - automount 文件系统的自动挂载点 - path 用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务 - # 查看系统是否安装了 systemd systemctl --version # 检查 systemd 是否运行 ps -ef | grep systemd # 如何查看上标中的类型(以 service 为例) systemctl -t service
二、systemctl 的简单介绍
-
systemctl命令与原来的service命令对比daemon 命令 systemctl 命令 说明 service [服务] start systemctl start [unit type] 启动服务 service [服务] stop systemctl stop [unit type] 停止服务 service [服务] restart systemctl 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.serivceP.S
systemctl daemon-reload并不是所有服务都支持这个参数,比如network.service -
systemctl命令与原来的chkconfig命令对比设置开机启动/不启动
daemon 命令 systemctl 命令 说明 chkconfig [服务] on systemctl enable [unit type] 设置服务开机启动 chkconfig [服务] off systemctl 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 -
systemctl命令与init命令对比init 命令 systemctl 命令 说明 init 0 systemctl poweroff 系统关机 init 6 systemctl reboot 重新启动 - systemctl suspend 进入睡眠模式 - systemctl hibernate 进入休眠模式 - systemctl rescue 强制进入救援模式 - systemctl emergency 强制进入紧急救援模式 -
设置系统运行级别
运行级别对照表
init 级别 systemctl target 0 shutdown.target 1 emergency.target 2 rescure.target 3 multi-user.target 4 - 5 graphical.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 在不重启的情况下,切换到图形界面下 -
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 的使用技巧
-
使用
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 -
关闭网络服务
在使用
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.socketP.S
由于Centos 7.x默认没有安装net-tools,因此无法使用netstat来查看主机开发的商品。需要通过yum安装来获得该工具包:# 安装 net-tools 工具 yum -y install net-tools # 查看是否关闭22端口 netstat -lnp |grep sshd -
关闭防火墙
firewallCentos 7.x中取消了iptables, 用firewall取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:systemctl stop firewalld.service systemctl disable firewalld.service
四、采用 systemctl 管理自定义服务
-
systemctl脚本存放的位置系统服务:开机不需要登录就能运行的程序(相当于开机自动启动)
/usr/lib/systemd/system用户服务:需要登录后才能运行的程序
/usr/lib/systemd/user -
初窥
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 -
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=forking Execstart 字段以 fox()方式启动,此时父进程将推出,子进程将成为主进程(后台运行),一般都设置为forkingType=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 中文手册》
-
-
采用
systemctl自定义Nginx服务-
创建
nginx.service文件# 进入 systemd 文件夹 cd /usr/lib/systemd/system # 创建 nginx.service 文件 vim nginx.service -
配置
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 -
采用
systemclt管理nginx.servicesystemctl enable|disable|status|start|restart|stop nginx.service
-
最后
以上就是瘦瘦跳跳糖最近收集整理的关于ContOS 7 下 Systemctl 的介绍和使用技巧的全部内容,更多相关ContOS内容请搜索靠谱客的其他文章。
发表评论 取消回复