系统服务管理工具systemd是为了便于linux用户操作系统服务的管理,systemd提供了systemctl命令工具进行systemd的各项操作。
Systemd的版本
systemd --version
systemd和systemctl的二进制文件和库的安装位置
whereis systemd
检查systemd是否正在运行
ps -eaf | grep systemd
检查单元(xxx.service)是否启用
systemctl is-enabled xxx.service
检查单元或服务状态
systemctl status firewalld.service
列出所有服务(包括启用和禁用)
systemctl list-unit-files --type=service
Linux中启动,重新启动,停止,重新加载和检查服务(httpd.service)的状态
systemctl start httpd.service
systemctl restart httpd.service
systemctl stop httpd.service
systemctl reload httpd.service
systemctl status httpd.service
引导时激活服务并启用或禁用服务(系统引导时自动启动服务)
systemctl is-active httpd.service
systemctl enable httpd.service
systemctl disable httpd.service
屏蔽(使其无法启动)或取消屏蔽服务(httpd.service)
systemctl mask httpd.service
systemctl unmask httpd.service
systemctl命令终止服务
systemctl kill httpd
使用Systemctl控制和管理挂载点
列出所有系统安装点
systemctl list-unit-files --type=mount
装载,卸载,重新装载,重新装载系统装载点,以及检查系统上装载点的状态
systemctl start tmp.mount
systemctl stop tmp.mount
systemctl restart tmp.mount
systemctl reload tmp.mount
systemctl status tmp.mount
在引导时激活,启用或禁用装入点(在系统引导时自动装入)
systemctl is-active tmp.mount
systemctl enable tmp.mount
systemctl disable tmp.mount
在Linux中屏蔽(使其无法启动)或取消屏蔽挂载点
systemctl mask tmp.mount
列出所有可用的系统套接字。
systemctl list-unit-files --type=socket
在Linux中启动,重新启动,停止,重新加载和检查套接字的状态(例如:cups.socket)
systemctl start cups.socket
systemctl restart cups.socket
systemctl stop cups.socket
systemctl reload cups.socket
systemctl status cups.socket
激活套接字并在引导时启用或禁用(在系统引导时自动启动套接字)
systemctl is-active cups.socket
systemctl enable cups.socket
systemctl disable cups.socket
屏蔽(使其无法启动)或取消屏蔽插座(cups.socket)
systemctl mask cups.socket
systemctl unmask cups.socket
检查服务的所有配置详细信息
systemctl show httpd
分析服务的关键链(httpd)
systemd-analyze critical-chain httpd.service
获取服务的依赖项列表(httpd)
systemctl list-dependencies httpd.service
按层次列出控制组
systemd-cgls
根据CPU,内存,输入和输出列出控制组
systemd-cgtop
重启,暂停,暂停,休眠或将系统置于混合睡眠状态
systemctl reboot
systemctl halt
systemctl suspend
systemctl hibernate
systemctl hybrid-sleep
注:unit操作
Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)
Unit 一共分成12种
Service unit:系统服务
Target unit:多个 Unit 构成的一个组
Device Unit:硬件设备
Mount Unit:文件系统的挂载点
Automount Unit:自动挂载点
Path Unit:文件或路径
Scope Unit:不是由 Systemd 启动的外部进程
Slice Unit:进程组
Snapshot Unit:Systemd 快照,可以切回某个快照
Socket Unit:进程间通信的 socket
Swap Unit:swap 文件
Timer Unit:定时器*
需要掌握的unit操作:
立即启动一个服务
systemctl start apache.service
立即停止一个服务
systemctl stop apache.service
重启一个服务
systemctl restart apache.service
杀死一个服务的所有子进程
systemctl kill apache.service
重新加载一个服务的配置文件
systemctl reload apache.service
重载所有修改过的配置文件
systemctl daemon-reload
显示某个 Unit 的所有底层参数
systemctl show httpd.service
显示某个 Unit 的指定属性的值
systemctl show -p CPUShares httpd.service
设置某个 Unit 的指定属性
sudo systemctl set-property httpd.service CPUShares=500
与日志有关操作
查看所有日志(默认情况下 ,只保存本次启动的日志)
journalctl
查看内核日志(不显示应用日志)
journalctl -k
查看系统本次启动的日志
journalctl -b
journalctl -b -0
查看上一次启动的日志(需更改设置)
journalctl -b -1
查看指定时间的日志
journalctl --since="2012-10-30 18:17:16"
journalctl --since "20 min ago"
journalctl --since yesterday
journalctl --since "2015-01-10" --until "2015-01-11 03:00"
journalctl --since 09:00 --until "1 hour ago"
显示尾部的最新10行日志
journalctl -n
显示尾部指定行数的日志
journalctl -n 20
实时滚动显示最新日志
journalctl -f
查看指定服务的日志
journalctl /usr/lib/systemd/systemd
查看指定进程的日志
journalctl _PID=1
查看某个路径的脚本的日志
journalctl /usr/bin/bash
查看指定用户的日志
journalctl _UID=33 --since today
查看某个 Unit 的日志
journalctl -u nginx.service
journalctl -u nginx.service --since today
实时滚动显示某个 Unit 的最新日志
journalctl -u nginx.service -f
合并显示多个 Unit 的日志
journalctl -u nginx.service -u php-fpm.service --since today
查看指定优先级(及其以上级别)的日志,共有8级
0: emerg
1: alert
2: crit
3: err
4: warning
5: notice
6: info
7: debug
journalctl -p err -b
日志默认分页输出,–no-pager 改为正常的标准输出
journalctl --no-pager
以 JSON 格式(单行)输出
journalctl -b -u nginx.service -o json
以 JSON 格式(多行)输出,可读性更好
journalctl -b -u nginx.serviceqq -o json-pretty
显示日志占据的硬盘空间
journalctl --disk-usage
指定日志文件占据的最大空间
journalctl --vacuum-size=1G
指定日志文件保存多久
journalctl --vacuum-time=1years