1.命令

1.systemctl

              systemctl list-unit-files --type service // 查看所有服务的开机自启状态
              ls /etc/systemd/system/*.wants/sshd.service  // 用来列出该服务在哪些运行级别下启用和禁用            

systemctl reboot // 重启系统
systemctl poweroff // 关闭系统,切断电源
systemctl halt // CPU停止工作
systemctl suspend // 暂停系统
systemctl hibernate // 让系统进入冬眠状态
systemctl hybrid-sleep // 让系统进入交互式休眠状态
systemctl rescue // 启动进入救援状态(单用户状态)

systemctl -t help //查看 Unit 类型
systemctl list-units // 列出正在运行的 Unit
systemctl list-units --all // 列出所有Unit,包括没有找到配置文件的或者启动失败的
systemctl list-units --all --state=inactive //列出所有没有运行的 Unit
systemctl list-units --failed //列出所有加载失败的 Unit
systemctl list-units --type=service //列出所有正在运行的、类型为 service 的 Unit

systemctl status // 显示系统状态
sysystemctl status bluetooth.servic //显示单个 Unit 的状态
systemctl -H [email protected] status httpd.service //显示远程主机的某个 Unit 的状态
systemctl is-active application.service // 显示某个 Unit 是否正在运行
systemctl is-failed application.service // 显示某个 Unit 是否处于启动失败状态
systemctl is-enabled application.service // 显示某个 Unit 服务是否建立了启动链接

        systemctl start apache.service //立即启动一个服务
systemctl stop apache.service //立即停止一个服务
systemctl restart apache.service //重启一个服务
systemctl kill apache.service //杀死一个服务的所有子进程
            systemctl reload apache.service //重新加载一个服务的配置文件
    systemctl daemon-reload //重载所有修改过的配置文件 
    systemctl show httpd.service //显示某个 Unit 的所有底层参数 
    systemctl show -p CPUShares httpd.service //显示某个 Unit 的指定属性的值
    systemctl set-property httpd.service CPUShares=500 //设置某个 Unit 的指定属性

    systemctl cat atd.service // 查看配置文件的内容


    systemctl list-dependencies nginx.service  //列出一个 Unit 的所有依赖
    systemctl list-dependencies --all nginx.service // 如果要展开 Target,就需要使用--all参数

    systemctl list-unit-files // 命令用于列出所有配置文件
    systemctl list-unit-files --type=service  //systemctl list-unit-files


2.systemd-analyze
systemd-analyze //查看启动耗时
systemd-analyze blame //查看每个服务的启动耗时
systemd-analyze critical-chain //显示瀑布状的启动过程流
systemd-analyze critical-chain atd.service//显示指定服务的启动流


3.hostnamectl
hostnamectl //显示当前主机的信息
hostnamectl set-hostname rhel7 //设置主机名


4.localectl
localectl //查看本地化设置
localectl set-locale LANG=en_GB.utf8 //设置本地化参数
localectl set-keymap en_GB //设置本地化参数


5.timedatectl
timedatectl // 查看当前时区设置
timedatectl list-timezones // 显示所有可用的时区
timedatectl set-timezone America/New_York 
    timedatectl set-time YYYY-MM-DD 
    timedatectl set-time HH:MM:SS


6.loginctl
loginctl list-sessions //列出当前session
loginctl list-users //列出当前登录用户
loginctl show-user ruanyf //列出显示指定用户的信息

2.Unit 的配置文件

/etc/systemd/system/

/usr/lib/systemd/system/

        /lib/systemd/system



每个配置文件有4中状态:
enabled:已建立启动链接
disabled:没建立启动链接
static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖
masked:该配置文件被禁止建立启动链接


如果配置文件被修改执行以下命令:
systemctl daemon-reload
systemctl restart httpd.service


66.Systemd 命令66.Systemd 命令66.Systemd 命令66.Systemd 命令


3.target

66.Systemd 命令


systemctl list-unit-files --type=target  //查看当前系统的所有 Target


systemctl list-dependencies multi-user.target  //查看一个 Target 包含的所有 Unit


systemctl get-default  //查看启动时的默认 Target


systemctl set-default multi-user.target  // 设置启动时的默认 Target
切换 Target 时,默认不关闭前一个 Target 启动的进程,


systemctl isolate 命令改变这种行为,
关闭前一个 Target 里面所有不属于后一个 Target 的进程


systemctl isolate multi-user.target


66.Systemd 命令

66.Systemd 命令


4.日志管理

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"
journalctl -n //显示尾部的最新10行日志
    journalctl -n 20 // 显示尾部指定行数的日志
    journalctl -f //实时滚动显示最新日志
    journalctl /usr/lib/systemd/systemd // 查看指定服务的日志
    journalctl _PID=1 // 查看指定进程的日志
    journalctl /usr/bin/bash // 查看某个路径的脚本的日志
    journalctl _UID=33 --since today // 查看指定用户的日志
    journalctl -u nginx.service // 查看某个 Unit 的日志


    # 查看指定优先级(及其以上级别)的日志,共有8级
# 0: emerg
# 1: alert
# 2: crit
# 3: err
# 4: warning
# 5: notice
# 6: info
# 7: debug
journalctl -p err -b


journalctl --no-pager // 日志默认分页输出,--no-pager 改为正常的标准输出
journalctl -b -u nginx.service -o json // 以 JSON 格式(单行)输出
journalctl -b -u nginx.serviceqq -o json-pretty  //以 JSON 格式(多行)输出,可读性更好


______________________________________________________________________________________



Systemd 入门教程:命令篇
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html




Systemd 入门教程:实战篇
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html




Systemd 定时器教程
http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html


CentOS7 启动流程与服务管理:


https://www.cnblogs.com/duzhaoqi/p/7582404.html


















相关文章:

  • 2021-06-09
  • 2021-10-11
猜你喜欢
  • 2021-07-29
  • 2021-10-07
  • 2021-07-18
相关资源
相似解决方案