systemctl命令

Systemd是一个命令组,涉及到系统管理的方方面面,而systemctlSystemd的主命令,用于管理系统。

描述

在历史上Linux的启动一直采用init进程,这种方法有两个缺点,一是启动时间长,init进程是串行启动,只有前一个进程启动完,才会启动下一个进程,二是启动脚本复杂,init进程只是执行启动脚本,不管其他事情,脚本需要自己处理各种情况,这往往使得脚本变得很长。
Systemd就是为了解决这些问题而诞生的,它的设计目标是,为系统的启动和管理提供一套完整的解决方案,根据Linux惯例,字母d是守护进程daemon的缩写,Systemd这个名字的含义,就是它要守护整个系统。使用了Systemd,就不需要再用init了,Systemd取代了initd,成为系统的第一个进程PID等于1,其他进程都是它的子进程,Systemd的优点是功能强大,使用方便,缺点是体系庞大,非常复杂,事实上,现在还有很多人反对使用Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反keep simple, keep stupidUnix哲学。

语法

systemctl [OPTIONS...] {COMMAND} ...

参数

OPTIONS

Unit Commands

Unit File Commands

Machine Commands

Job Commands

Snapshot Commands

Environment Commands

Manager Lifecycle Commands

System Commands

UNIT文件配置

UNIT文件类型

Unit文件统一了过去各种不同的系统资源配置格式,例如服务的启动、停止、定时任务、设备自动挂载、网络配置、设备配置、虚拟内存配置等,而Systemd通过不同的通过文件的后缀名来区分这些配置文件,.service文件便是其中最常用的一种,下面是Systemd所支持的12Unit文件类型。

配置目录

Unit文件按照Systemd约定,应该被放置在指定的3个系统目录之一,这3个目录是有优先级的,在下面指定的位置优先级依次递减,因此在几个目录中有同名文件的时候,只有优先级最高的目录里的那个会被使用。

Service文件字段

通常我们只需要配置.service文件即可,.service分为三个字符。

Unit段

这些配置中,除了Description外,都能够被添加多次,例如After参数可以使用空格分隔指定所有值,也可以使用多个After参数,在每行参数中指定一个值。

Install段

这个段中的配置与Unit有几分相似,但是这部分配置需要通过systemctl enable命令来激活,并且可以通过systemctl disable命令禁用,另外这部分配置的目标模块通常是特定启动级别的.target文件,用来使得服务在系统启动时自动运行。

Service段

这个段是.service文件独有的,也是对于服务配置最重要的部分,这部分的配置选项非常多,主要分为服务生命周期控制和服务上下文配置两个方面,下面是一些常用的配置,另外还有一些限制特定服务可用的系统资源量,例如CPU、程序堆栈,文件句柄数量,子进程数量等等,可参考Linux文档资源配额。

文件示例

[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
Alias=sshd.service

示例

开机运行服务。

systemctl enable nginx.service

查询服务是否开机启动。

systemctl is-enabled nginx.service

取消开机运行。

systemctl disable nginx.service

启动服务。

systemctl start nginx.service

停止服务。

systemctl stop nginx.service

重启服务。

systemctl restart nginx.service

重新加载服务配置文件,修改配置文件后需要首先执行systemctl daemon-reload

systemctl reload nginx.service

查询服务运行状态。

systemctl status nginx.service

显示启动失败的服务。

systemctl --failed
systemctl list-units --state failed

重置单元的启动频率计数器。

systemctl reset-failed

重新加载所有被修改过的服务配置,否则配置不会生效,当然实际上该命令还会完成很多事情,例如重新生成依赖树。

systemctl daemon-reload

输出所有单元。

systemctl list-units

列出所有单元状态概览。

systemctl status

查看已激活的服务。

systemctl list-units -t service

查看所有已安装服务。

systemctl list-unit-files

检查nginx服务的所有配置细节。

systemctl show nginx.service

获取nginx服务的依赖性列表。

systemctl list-dependencies nginx.service

查看环境变量。

systemctl show-environment

重启系统。

systemctl reboot

关闭系统。

systemctl poweroff

CPU停止工作。

systemctl halt

暂停系统。

systemctl suspend

让系统进入冬眠状态。

systemctl hibernate

让系统进入交互式休眠状态。

sudo systemctl hybrid-sleep

启动进入救援状态,即单用户状态。

systemctl rescue

参考

https://www.cnblogs.com/tsdxdx/p/7288490.html
https://www.cnblogs.com/chjbbs/p/6288592.html
https://www.geeksforgeeks.org/systemctl-in-unix/
http://www.jinbuguo.com/systemd/systemd.unit.html
https://blog.csdn.net/m0_38023255/article/details/78757655
https://www.commandlinux.com/man-page/man1/systemctl.1.html
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html