Systemd介绍
Systemd:
- 为系统的启动和管理提供一套完整的解决方案。 优点是功能强大, 使用方便。 缺点是体系庞大, 非常复杂。 Systemd 并不是一个命令, 而是一组命令, 涉及到系统管理的方方面面。
- systemctl: Systemd的主命令, 用于管理系统。
- systemctl reboot (重启系统)
- systemctl poweroff (关闭系统)
- systemctl halt (CPU停止工作)
- systemctl suspend (暂停系统)
- systemctl hiberate (系统进入冬眠模式系统)
- systemctl hybrid-sleep (进入交互式休眠状态)
- systemctl rescue (启动进入救援状态, 单用户状态)
- systemd-analyze: 查看启动耗时。
- hostnamectl: 查看当前主机的信息
- localectl: 查看本地化设置
- timedatectl: 查看当前时区设置。
- loginctl: 查看当前登录的用户。
Systemd可以管理所有系统资源, 不同的资源统称为Unit (单位)
- systemctl list-units 查看当前系统的所有unit
- systemctl status 查看系统状态和单个unit的状态
- systemctl unit管理
- systemctl start
- systemctl stop
- systemctl restart
- systemctl kill
- systemctl reload 重新加载一个服务的配置文件
- systemctl daemon-reload 显示所有修改过的配置文件
- systemctl show httpd.service 显示某个unit的指定属性的值
- systemctl set-property httpd.service 设置某个unit的指定属性
- unit之间的依赖关系: systemctl list-dependencies nginx.service systemctl list-dependencies --all nginx.service
- unit的配置文件
每一个Unit都有一个配置文件, 告诉Systemd怎么启动这个Unit. systemd默认从目录 /etc/systemd/system/读取配置文件。 但是里面存放的大部分文件是符号链接, 指向目录 /usr/lib/systemd/system/. 真正的配置文件存放在那个目录。
$ systemctl cat atd.service [Unit] Description=ATD daemon [Service] Type=forking ExecStart=/usr/bin/atd [Install] WantedBy=multi-user.target
[Unit] 区块通常是配置文件的第一个区块, 用来定义Unit的元数据, 以及配置与其他Unit的关系
- Description: 简短描述
- Documentation: 文档地址
- Requires: 当前Unit依赖的其他Unit, 如果它们没有运行, 当前Unit会启动失败
- Wants: 当前Unit配合的其他Unit, 如果它们没有运行, 当前Unit不会启动失败
- BindsTo:
- Before
- After
- Conflicts
- Condition
- Assert
[Install] 通常是配置文件的最后一个区块,用于定义如何启动, 以及是否开机启动。
- WantBy:
- RequireBy:
- Alias:
- Also:
[Service] 区块用来Service的配置, 只有Service类型的Unit才有这个区块。
- Type
- simple: 默认值, 执行ExecStart指定的命令, 启动主进程。
- forking: 以fork方式从父进程创建子进程, 创建后父进程会立即退出
- oneshot: 一次性进程, Systemd会等当前服务退出, 再继续往下执行
- dbus: 当前服务通过dbus启动
- notify: 当前服务启动完毕, 会通知systemd, 再继续往下执行。
- idle: 若有其他任务执行完毕, 当前服务才会进行。
- ExecStart
- ExecStartPre
- ExecStartPost
- ExecReload
- ExecStop
- ExecStopPost
- RestartSec
- Restart : always, on-success, on-failure, on-abnormal, on-abort, on-watchdog
- TimeoutSec
- Environment
journalctl