在 stackoverflow.com 的Unix & Linux 社区 看到有小伙伴提出相关的问题。
这里整理分享给小伙伴,博文内容涉及:
Systemd 的 Path Units常用命令手册学习
Path Units 的应用
通过httpd服务演示监听配置文件自动重启服务
食用方式:需要了解一点 Systemd
理解不足小伙伴帮忙指正
在路上,我们永远年轻,永远热泪盈眶。 ——杰克·凯鲁亚克
了解 Systemd 的 Path Units
在这之前,需要简单的了解一下 Systemd 的 Path Units ,大概熟悉下是什么,Path Units 和我们经常讲的服务 Service units(sshd.service) 同属于 Systemd 的 Units。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
┌──[root@vms83.liruilongs.github.io]-[~] └─$systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 一 2022-10-24 09:41:59 CST; 1 day 2h ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 984 (sshd) Memory: 4.5M CGroup: /system.slice/sshd.service └─984 /usr/sbin/sshd -D
10月 24 09:41:58 vms83.liruilongs.github.io systemd[1]: Starting OpenSSH server daemon... 10月 24 09:41:59 vms83.liruilongs.github.io sshd[984]: Server listening on 0.0.0.0 port 22. 10月 24 09:41:59 vms83.liruilongs.github.io sshd[984]: Server listening on :: port 22. 10月 24 09:41:59 vms83.liruilongs.github.io systemd[1]: Started OpenSSH server daemon. ..........
Service Units 用于封装一个后台服务进程,而 Path Units 用于根据文件或目录变化来启动其他服务,类似一个监听器,面向对象里的观察者设计模式。这里他的通知对象时一个服务(service units)
常用命令
看一些常用的命令,查看当前 Systemd 的所有 Path Units
1 2 3 4 5 6 7 8 9
┌──[root@vms83.liruilongs.github.io]-[~] └─$systemctl list-unit-files -t path UNIT FILE STATE brandbot.path disabled systemd-ask-password-console.path static systemd-ask-password-plymouth.path static systemd-ask-password-wall.path static
┌──[root@vms83.liruilongs.github.io]-[~] └─$man systemd | grep -A 2 "Path units" 10. Path units may be used to activate other services when file system objects change or are modified. See systemd.path(5).
┌──[root@vms83.liruilongs.github.io]-[~] └─$man systemd.path | cat SYSTEMD.PATH(5) systemd.path SYSTEMD.PATH(5) 。。。。 NAME systemd.path - Path unit configuration
SYNOPSIS path.path
DESCRIPTION # 以 ".path" 为后缀的单元文件, 封装了一组由 systemd 监视的文件系统路径,以支持基于路径的启动。 A unit configuration file whose name ends in".path" encodes information about a path monitored by systemd, for path-based activation. # 本手册列出了所有专用于此类单元的 配置选项(亦称"配置指令"或"单元属性")。 systemd.unit(5) 中描述了通用于所有单元类型的配置选项, # 它们位于 [Unit] 与 [Install] 小节。此类单元专用的配置选项 位于 [Path] 小节。 This man page lists the configuration options specific to this unit type. See systemd.unit(5) for the common options of all unit configuration files. The common configuration items are configured in the generic [Unit] and [Install] sections. The path specific configuration options are configured in the [Path] section. # 每个路径单元都必须有一个与其匹配的单元, 以用于在路径发生变化时启动。 匹配的单元可以通过 Unit= 选项(见下文)明确指定。 # 若未指定,则默认是与该单元名称相同的 .service 单元(不算后缀)。 例如 foo.path 默认匹配 foo.service 单元。 For each path file, a matching unit file must exist, describing the unit to activate when the path changes. By default, a service by the same name as the path (except for the suffix) is activated. Example: a path file foo.path activates a matching service foo.service. The unit to activate may be controlled by Unit= (see below). # 因为在单元内部实际上使用内核的 inotify(7) 函数监视文件系统的变化, 所以,受制于 inotify 的缺陷, # 只能监视本机文件系统的变化, 而不能监视远程网络文件系统的变化。 Internally, path units use the inotify(7) API to monitor file systems. Due to that, it suffers by the same limitations as inotify, and for example cannot be used to monitor files or directories changed by other machines on remote NFS file systems.
If a path unit is beneath another mount point in the file system hierarchy, a dependency between both units is created automatically. # 除非明确设置了 DefaultDependencies=no ,否则 path 单元将会自动添加下列依赖关系:
Unless DefaultDependencies=false is used, path units will implicitly have dependencies of type Conflicts= and Before= on shutdown.target. These ensure that path units are terminated cleanly prior to system shutdown. Only path units involved with early boot or late system shutdown should disable this option.
OPTIONS # 路径单元文件必须包含一个 [Path] 部分,其中包含有关它监视的一个或多个路径的信息。 # 特定于路径单元的 [Path] 部分的选项如下: Path files must include a [Path] section, which carries information about the path(s) it monitors. The options specific to the [Path] section of path units are the following:
Defines paths to monitor for certain changes: PathExists= may be used to watch the mere existence of a file or directory. If the file specified exists, the configured unit is activated. PathExistsGlob= works similar, but checks for the existence of at least one file matching the globbing pattern specified. PathChanged= may be used to watch a file or directory and activate the configured unit whenever it changes. It is not activated on every write to the watched file but it is activated if the file which was open for writing gets closed. PathModified= is similar, but additionally it is activated also on simple writes to the watched file. DirectoryNotEmpty= may be used to watch a directory and activate the configured unit whenever it contains at least one file.
# 所有这些选项的值 都必须是绝对路径。 The arguments of these directives must be absolute file system paths.
# 可以多次使用这些选项 以监控多个路径。 如果为某个选项指定了一个空字符串, 则表示清空该选项 之前设置的所有监视路径。 Multiple directives may be combined, of the same and of different types, to watch multiple paths. If the empty string is assigned to any of these options, the list of paths to watch is reset, and any prior assignments of these options will not have any effect.
# 如果在启动 path 单元时, 指定的路径已经存在(对于 PathExists= 与 PathExistsGlob= 来说) # 或者 指定的目录非空(对于 DirectoryNotEmpty= 来说), 那么将会立即同时启动匹配单元。 不过, # 对于 PathChanged= 与 PathModified= 来说,并不遵守这个规则。 If a path already exists (incase of PathExists= and PathExistsGlob=) or a directory already is not empty (incase of DirectoryNotEmpty=) at the time the path unit is activated, then the configured unit is immediately activated as well. Something similar does not apply to PathChanged= and PathModified=.
# 如果没有权限监视指定的路径, 那么 systemd 将会 一直等待权限满足之后 才会开始监视。 If the path itself or any of the containing directories are not accessible, systemd will watch for permission
changes and notice that conditions are satisfied when permissions allow that.
Unit= # 该路径单元的匹配单元, 也就是被路径的变化启动的单元。 参数是一个不以 ".path" 结尾的单元名。 # 默认值是与此路径单元同名的服务单元(见上文)。 # 建议将路径单元的名字 与被该路径启动的匹配单元的名字 保持一致 (也就是仅单元后缀名不同)。 The unit to activate when any of the configured paths changes. The argument is a unit name, whose suffix is not ".path". If not specified, this value defaults to a service that has the same name as the path unit, except for the suffix. (See above.) It is recommended that the unit name that is activated and the unit name of the path unit are named identical, except for the suffix.
MakeDirectory= # 接受一个布尔值。 设为 yes 表示如果指定的目录不存在则首先创建它然后再监视它。 此选项对 PathExists= 无效。 默认值为 no Takes a boolean argument. If true, the directories to watch are created before watching. This option is ignored for PathExists= settings. Defaults to false.
DirectoryMode= # 选项指定在 MakeDirectory= 时新建目录的权限(八进制表示法)。 默认值是 0755 If MakeDirectory= is enabled, use the mode specified here to create the directories in question. Takes an access mode in octal notation. Defaults to 0755.
SEE ALSO systemd(1), systemctl(1), systemd.unit(5), systemd.service(5), inotify(7), systemd.directives(7)
┌──[root@vms82.liruilongs.github.io]-[/usr/lib/systemd/system] └─$fg vim update-user-info.path ┌──[root@vms82.liruilongs.github.io]-[/usr/lib/systemd/system] └─$cat /usr/lib/systemd/system/update-user-info.path [Unit] Description="Monitor the /etc/passwd file for changes"
┌──[root@vms82.liruilongs.github.io]-[/usr/lib/systemd/system] └─$systemctlenable update-user-info.path --now Created symlink from /etc/systemd/system/multi-user.target.wants/update-user-info.path to /usr/lib/systemd/system/update-user-info.path.
┌──[root@vms82.liruilongs.github.io]-[/usr/lib/systemd/system] └─$journalctl -u update-user-info.path -- Logs begin at 一 2022-10-24 09:41:47 CST, end at 二 2022-10-25 17:57:25 CST. -- 10月 25 16:37:42 vms82.liruilongs.github.io systemd[1]: Started "Monitor the /etc/passwd file for chang 10月 25 16:37:42 vms82.liruilongs.github.io systemd[1]: Starting "Monitor the /etc/passwd file for chan
1 2 3 4 5
┌──[root@vms82.liruilongs.github.io]-[/usr/lib/systemd/system] └─$journalctl -u update-user-info.service -- Logs begin at 一 2022-10-24 09:41:47 CST, end at 二 2022-10-25 17:57:31 CST. -- 10月 25 16:38:26 vms82.liruilongs.github.io systemd[1]: Started "Run script to send email alert". 10月 25 16:38:26 vms82.liruilongs.github.io systemd[1]: Starting "Run script to send email alert"...