Linux 系统资源管理 | systemd
就系统中通常执行 /etc/init.d/ 目录下的文件来管理服务。
新系统中可统一使用 systemctl 组命令来管理服务。
systemctl 用法
最常用来启动、停止服务
-
systemctl status nginx查看 nginx 服务状态 -
systemctl start nginx启动 nginx 服务 -
systemctl stop nginx停止 nginx 服务 -
systemctl reload nginx重载 nginx 配置 -
systemctl restart nginx重启 nginx 服务 -
systemctl enable nginx设置 nginx 开机自启动
systemctl 是 systemd 的主命令,用来管理系统
-
systemctl reboot重启系统 -
systemctl poweroff关机 -
systemctl suspend暂停系统 -
systemctl hibernate休眠 -
systemctl hybrid-sleep交互式休眠 -
systemctl rescue进入救援状态(单用户状态) -
systemctl haltCPU 停止工作
其它常用命令
-
systemctl status显示系统状态 -
systemctl -H root@dev.com status nginx查看远程主机的 nginx 状态 -
systemctl is-active nginx查看 nginx 是否运行 -
systemctl is-failed nginx查看 nginx 是否启动失败 -
systemctl is-enabled nginx查看 nginx 是否开机自启动 -
systemctl daemon-reload重载所有修改过的配置文件 -
systemctl kill nginx杀死 nginx 所有子进程 -
systemctl mask nginx冻结 nginx 服务 -
systemctl unmask nginx启用 nginx 服务 -
systemctl show nginx显示 nginx 的所有底层参数 -
systemctl show -p CPUShares nginx显示 nginx 的指定属性的值 -
systemctl set-property nginx CPUShares=500设置 nginx 的指定属性
unit 管理
systemd 可以管理所有系统资源,统称为 unit
查看系统所有资源
-
systemctl list-units --all列出所有Unit -
systemctl list-units列出正在运行的 Unit -
systemctl list-units --type=service列出正在运行的 service -
systemctl list-units --all --state=inactive列出所有没有运行的 Unit -
systemctl list-units --failed列出所有加载失败的 Unit
资源类型
通过上面查看资源的命令可以看到有不同的资源
-
.automount自动挂载点 -
.device硬件设备 -
.mount文件系统的挂载点 -
.path文件或路径 -
.scope不是由 Systemd 启动的外部进程 -
.service系统服务 -
.slice进程组 -
.snapshotSystemd 快照,可以切回某个快照 -
.socket进程间通信的 socket -
.swapswap 文件 -
.target多个 Unit 构成的一个组 -
.timer定时器
资源依赖
unit 之间存在依赖关系,比如启动 perl-fastcgi 之前需启动 nginx
-
systemctl list-dependencies nginx列出 nginx 服务的所有依赖 -
systemctl list-dependencies --all nginx展开显示 nginx 服务的所有依赖
service 状态
loaded 系统服务已经初始化完成,加载过配置
active(running)执行中
active(exited) 仅执行一次就正常的服务
inactive 服务关闭
enbaled 服务开机启动
disabled 服务开机不自启
static 服务开机启动项不可被管理
failed 系统配置错误
资源配置
每个 unit 都有一个配置文件,说明启动停止等方式
-
配置文件默认在
/etc/systemd/system各目录下,并软连接到/usr/lib/systemd/system目录 -
enable 设置自启动就会创建两个目录的软连接
-
相反,disable 会删除软连接,关闭开机自启动
-
systemd 也会去
/etc/init.d/查找、运行相关资源 -
查看所有配置文件
# 列出所有配置文件 systemctl list-unit-files # 列出类型为 service 的配置文件 systemctl list-unit-files --type=service -
配置文件状态
static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖 enabled:已建立启动链接 disabled:没建立启动链接 masked:该配置文件被禁止建立启动链接
systemd 还有其它内容,学习后再来补充

浙公网安备 33010602011771号