Linux:systemctl指令

发布时间 2023-07-26 14:36:44作者: ShineLe

1、简介

用于管理进程:

  • CentOS 5:SysV init;
  • CentOS 6:Upstart;
  • CentOS 7:Systemd;

CentOS7采用systemd管理守护进程服务独立地运行于内存中,服务响应速度快,但占用更多内存

独立服务的服务启动脚本存放于目录/usr/lib/systemd/system中。

Systemd的新特性:

  • 系统引导时实现服务的并行启动;
  • 按需激活进程;
  • 系统实现快照;
  • 基于依赖关系定义服务的控制逻辑;

systemctl可用于内省控制systemd系统服务管理器的状态。CentOS7.x系统环境下我们使用该命令启停服务,实际上该命令除了独立服务还有很多其他用途。

2、用法

systemctl 选项 [服务]

选项

用法:systemctl start unit

说明

start 启动unit
stop 停止
restart 重启,先stop再start
reload 重载配置文件,不stop服务
enable 开机自启动
disbale 取消开机自启动
status unit的状态
is-active 是否活跃/运行
is-enable 是否开机启动
kill 杀死服务
show 列出unit的配置
mask 注销unit,之后再也无法启动unit
unmask 取消对unit的注销
list-units

列出所有目前启动的unit。

加参数-all同时列出没启动的。

list-unit-files

列出所有已安装的unit以及它们的开机启动状态

(enabled,disabled,static,mask)

--type=TYPE unit type,包括service、socket、target
get-default 当前target
set-default 设定后面的target为默认的操作模式
isolate 切换到后面的模式
snapshot 系统快照
delete 删除快照
show-environment 展示环境变量
poweroff 关闭系统
reboot 重启机器
suspend 系统睡眠
get-default 查看系统启动模式

例子

1)帮助:systemctl --help

2)启动:systemctl start unit

3)状态:systemctl status unit

4)停止:systemctl stop unit

5)活跃检查:systemctl is-active unit

6)重新加载配置:systemctl reload unit

7)重启服务(先stop,然后start):systemctl restart unit

8)列出所有可用unit:systemctl list-unit-files

9)列出所有已加载unit:systemctl list-units [--type=TYPE]

 10查看所有systemctl管理的所有服务(即service文件):--type=service

 11)注销:systemctl mask unit

服务注销后,就无法通过systemctl进行启停管理了

[root@s153 system]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@s153 system]# systemctl start firewalld
Failed to start firewalld.service: Unit is masked.

12)取消注销:systemctl unmask unit

13)开机自启动:systemctl enable unit

14)取消开机自启动:systemctl disable unit

15)机器信息:systemctl list-machines

16)环境变量:systemctl show-environment

17)重新加载unit:systemctl daemon-reload

如果手动修改了unit,使用该命令重新加载

18)系统快照:systemctl sanapshot unit

19)删除快照:systemctl delete unit

20)检查是否开机自启动:systemctl is-enabled unit

21)杀死服务:systemctl kill unit

22)切换救援模式:systemctl rescue

切换默认模式:systemctl default

23)关闭系统:systemctl poweroff

24)重启机器:systemctl reboot

25)系统睡眠/挂起:systemctl suspend

26)查看系统启动模式:systemctl get-default

3、unit的type:--type

  • Service: 文件扩展名.service, 用于定义系统服务;
  • Target: 文件扩展名.target, 用于模拟实现"运行级别";
  • Device: 文件扩展名.device, 用于定义内核识别的设备;
  • Mount: 文件扩展名.mount, 用于定义文件系统的挂载点;
  • Socket: 文件扩展名.socket, 用于标识进程间通信用到的socket文件;
  • Snapshot: 文件扩展名.snapshot, 用于管理系统快照;
  • Swap: 文件扩展名.swap, 用于标识swap设备;
  • Automount: 文件扩展名.automount, 用于定义文件系统自动点设备;
  • Path: 文件扩展名.path, 用于定义文件系统中的一文件或目录;

4、unit file的结构

文件通常由三部分组成:

[Unit]: 定义与Unit类型无关的通用选项;用于提供unit的描述信息,unit行为及依赖关系等。
[Service]:与特定类型相关的专用选项;此处为Service类型。
[Install]:定义由"systemctl enable"及"systemctl disable"命令在实现服务启用或禁用时用到的一些选项。

一个unit文件的示例

[root@s153 system]# cat chronyd.service
[Unit]
Description=NTP client/server
Documentation=man:chronyd(8) man:chrony.conf(5)
After=ntpdate.service sntp.service ntpd.service
Conflicts=ntpd.service systemd-timesyncd.service
ConditionCapability=CAP_SYS_TIME

[Service]
Type=forking
PIDFile=/var/run/chronyd.pid
EnvironmentFile=-/etc/sysconfig/chronyd
ExecStart=/usr/sbin/chronyd $OPTIONS
ExecStartPost=/usr/libexec/chrony-helper update-daemon
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=full

[Install]
WantedBy=multi-user.target

 

5、Unit段的常用选项

Description:描述信息,意义性描述;
After:定义unit的启动次序;表示当前unit应晚于哪些unit启动;其功能与Before相反;
Requies:依赖到其它的units;强依赖,被依赖的units无法激活时,当前的unit即无法激活;
Wants:依赖到其它的units;弱依赖;
Confilcts:定义units 的冲突关系;

6、Service段的常用选项

Type:用于定义影响ExecStart及相关参数的功能的unit进程类型;
类型有:simple、forking、oneshot、dbus、notify、idle。
EnvironmentFile:环境配置文件;
ExecStart:指明启动unit要运行的命令或脚本;ExecStart, ExecStartPost
ExecStop:指明停止unit要运行的命令或脚本;
Restart:

7、Install段的常用配置

Alias:
RequiredBy:被哪些unit所依赖;
WantBy:被哪些unit所依赖;