「systemd」 - 创建和修改单元文件 @20210418

内容简介

作为一个系统管理员,要遵循系统管理规范,而不能说”能用就行“。本文将介绍如何编写systemd单元文件。

前言

这不是一个详细的教程。这篇文章是在我学习如何编写systemd单元文件时的一个摘要笔记。文章的内容只是一个概括,涵盖了创建单元文件可能用到的知识点和相关的参考手册,并不涉及太多的细节,细节上的东西只能去读文档了。

单元文件的存储位置

单元文件的保存位置:Table 10.2, “Systemd Unit Files Locations
/usr/lib/systemd/system/
Systemd unit files distributed with installed RPM packages.

/run/systemd/system/
Systemd unit files created at run time. This directory takes precedence over the directory with installed service unit files.

/etc/systemd/system/
Systemd unit files created by systemctl enable as well as unit files added for extending a service. This directory takes precedence over the directory with runtime unit files. the /etc/systemd/system/ directory is reserved for unit files created or customized by the system administrator.

单元文件的命名方式

形如:unit_name.type_extension

unit_name:单元名。
type_extension:单元类型。可用的类型参考:Table 10.1, “Available systemd Unit Types

单元文件结构

单元文件由[Unit]、[unit type]、[Install]三部分组成,每个部分都包含一些指令。

[Unit]

包含描述、依赖等指令,是一些更通用的指令,这些指令与单元类型无关。

该部分中可用的指令参考Table 10.9, “Important [Unit] Section Options

[unit type]

参数unit type不是固定的,可以是ServiceTimer等等,它代表了单元文件的类型。比如,如果服务类型的单元文件,则该部分应为[Service],而对于定时任务类型的单元文件,该部分应为[Timer]

与[Service]相关的指令可以参考Table 10.10, “Important [Service] Section Options”,或者参考system.service(5)手册。

[Install]

单元文件安装相关的信息。所谓”安装“指systemctl enablesystemctl disable命令。该部分中可用的指令参考Table 10.11, “Important [Install] Section Options

与某个单元文件相关的目录

除了直接修改单元文件外,也可以加入自定义的配置选项,但这是通过使用补充目录实现的。参考Section 10.6.4, “Modifying Existing Unit Files

也可以通过补充目录的方式创建服务的依赖项目。补充目录里保存了当前服务所依赖的服务。

单元文件中使用参数

单元文件中还可以使用命令行传入的参数。参考Section 10.6.5, “Working with Instantiated Units

注意事项

修改了单元文件之后,要运行systemctl daemon-reload命令来重新加载systemd管理配置。

相关链接

systemd.unit — Unit configuration

相关文章

「Supervisor」- 创建首个配置文件
「Supervisor」- 配置与管理
「systemd」- 配置文件
「Supervisor」- A Process Control System
「systemd」- 常见错误汇总
「Supervisor」- 安装
使用systemd在开机时自动挂在硬盘

参考文献

Systemd 入门教程:命令篇
systemd.unit — Unit configuration
Creating and Modifying systemd Unit Files
更多的用法可以参考官方的手册,或者systemd.unt(5)手册
Do systemd unit files have to be reloaded when modified?


posted @ 2021-04-18 22:35  研究林纳斯写的  阅读(196)  评论(0编辑  收藏  举报