介绍
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。
在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非总是如此。
systemd总是其它进程的父进程,它的PID总是为1,作为系统引导程序。
$ ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 195672 4892 ? Ss 6月27 13:26 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
systemd常用命令
一、分析各进程启动时花费的时间
$ systemd-analyze blame
7.671s NetworkManager-wait-online.service
4.288s kdump.service
2.927s mariadb.service
2.856s plymouth-quit-wait.service
548ms boot.mount
544ms postfix.service
. . .
二、列出所有已安装的单元
可在结尾加入
--type=service
等过滤要列出的单元
$ systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled
cups.path enabled
systemd-ask-password-console.path static
. . .
三、列出所有已加载的单元
$ systemctl list-units
UNIT LOAD ACTIVE SUB
proc-sys-fs-binfmt_misc.automount loaded active running
sys-devices-pci0000:00-0000:00:02.0-0000:01:00.1-sound-card1.device loaded active plugged
sys-devices-pci0000:00-0000:00:04.0-0000:02:00.0-net-enp2s0.device loaded active plugged
sys-devices-pci0000:00-0000:00:11.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged
sys-devices-pci0000:00-0000:00:11.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged
sys-devices-pci0000:00-0000:00:11.0-ata3-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged
sys-devices-pci0000:00-0000:00:14.2-sound-card0.device loaded active plugged
四、检查某个服务是否启动
$ systemctl is-enabled mariadb
disabled
五、服务的开机启动、开始、停止、状态、重载等操作
以mariadb为例:
# 开启开机启动
$ systemctl enable mariadb
# 关闭开机启动
$ systemctl disable mariadb
# 开始服务
$ systemctl start mariadb
# 停止服务
$ systemctl stop mariadb
# 重载服务
$ systemctl reload mariadb
# 重启服务
$ systemctl restart mariadb
# 检测服务状态,加-l参数可以得到完整的状态日志
$ systemctl status mariadb
# 检查服务是否激活
$ systemctl is-active mariadb
六、显示或屏蔽一个服务
# 显示一个服务
$ systemctl mask mariadb.service
# 屏蔽一个服务
$ systemctl unmask mariadb.service
七、结束一个服务
$ systemctl kill mariadb
八、检测服务配置
$ systemctl show mariadb
九、控制系统运行级别
# 获取当前系统运行级别
$ systemctl get-default
# 启动某个运行级别,如图形模式
$ systemctl isolate runlevel5.target
# 等同
$ systemctl isolate graphical.target
系统运行级别:
Runlevel 0 | poweroff.target: 关闭系统
Runlevel 1 | rescue.target: 单用户模式
Runlevel 2 | runlevel4.target | multi-user.target: 用户定义/域特定运行级别。默认等同于 3。
Runlevel 3 | multi-user.target: 多用户,无图形系统
Runlevel 4 | multi-user.target: 多用户,无图形系统
Runlevel 5 | graphical.target: 多用户,图形化系统
Runlevel 6 | reboot.target: 关闭并重启机器
十、重启、关机、挂起等操作
# 重启
$ systemctl reboot
# 关机
$ systemctl halt
# 挂起
$ systemctl suspend
# 休眠
$ systemctl hibernate
# 混合休眠
$ systemctl hybrid-sleep
编写自己的系统服务
systemd的服务都存放在/usr/lib/systemd/目录中
以MariaDB为栗子,打开mariadb.service,可以看到如下内容:
[Unit]
Description=MariaDB database server
After=syslog.target
After=network.target
[Service]
Type=simple
User=mysql
Group=mysql
ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n
# Note: we set --basedir to prevent probes that might trigger SELinux alarms,
# per bug #547485
ExecStart=/usr/bin/mysqld_safe --basedir=/usr
ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
# Place temp files in a secure directory, not /tmp
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit] 部分用于服务说明
Description
描述服务说明
After
描述服务类别
[Service] 部分用于管理单元的服务和进程的关键配置
type 用于描述服务类型
Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。
Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。
Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。
Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。
User和Group 分别是指定该服务所属的用户和用户组。
ExecStartPre 服务开始之前执行的命令。
ExecStart 服务的详细执行命令。
ExecStartPost 在ExecStart执行成功之后执行的命令。
ExecReload 重新加载服务时执行的命令。
ExecStop 停止服务执行的命令。
TimeoutSec 超时时间。
PrivateTmp 是否替换临时文件路径到一个安全目录。
PIDFile设置指定服务的Pid文件保存路径,一般用于单进程服务,防止启动多个服务。
[Install] 是服务安装的相关设置,可设置为多用户的
WantedBy=multi-user.target