Systemd: Service File Examples

大多数Linux发行版使用 systemd作为系统服务管理工具。

systemctl是systemd的主要命令,用于管理控制服务。

这篇文章中将介绍如何创建允许你使用systemctl命令的sysytemd服务文件,如何无需reboot来重启systemd并reload unit文件,如何enable 一个新的服务。

并举例介绍大多数重要的systemd服务文件选项。

创建 systemd service 文件

创建systemd service file /etc/systemd/system/name.service (name 是示例名称,根据你自己的服务名称自行替换)

准备使用自定义服务可执行文件。这可以是自定义创建的脚本,也可以是软件提供商提供的可执行文件。如果需要,准备一个PID文件以保存自定义服务主进程的常量PID。还可以包含环境文件来存储服务的shell变量。确保源脚本是可执行的(通过执行)而不是交互式的。

touch /etc/systemd/system/name.service
chmod 664 /etc/systemd/system/name.service

打开该文件,添加systemd最小服务配置选项:

[Unit]
Description=service_description
After=network.target

[Service]
ExecStart=path_to_executable
Type=forking
PIDFile=path_to_pidfile

[Install]
WantedBy=default.target

其中:

  • service_description 是一个信息性描述,显示在日志日志文件和systemctl status命令的输出中。
  • 该设置确保仅在网络运行后启动服务。添加以空格分隔的其他相关服务或目标列表 After
  • path_to_executable 代表实际服务可执行文件的路径
  • Type=forking用于进行fork系统调用的守护进程。使用 path_to_pidfile 中指定的PID创建服务的主进程。查找其他启动类型

service 文件发生变更,systemd配置需要重新加载:

$ sudo systemctl daemon-reload

现在,应可以使用 start, stop, restartstatus:

$ sudo systemctl start name
$ sudo systemctl stop name
$ sudo systemctl restart name
$ systemctl status name

在系统启动时自动配置服务,使用 enable

$ sudo systemctl enable name

检查 service 日志:

$ journalctl -u name

Systemd Service File Options 选项

Systemd service files通常含有三个区域。

通用配置项在一般在[Unit][Install]配置

特定于服务的配置选项在 [Service]中进行配置

Important Section Options[Unit]

Option Description
Description unit的简短描述
Documentation 参考文档URI列表
Before, After units 的启动顺序
Requires 如果激活此unit,则此处列出的units也将被激活。如果其他units中的一个停用或发生故障,则此unit将被停用。
Wants 配置较Requires弱的依赖性。如果列出的任何unit未成功启动,则对此unit激活没有影响。这是建立自定义unit依赖关系的推荐方法。
Conflicts 如果一个 unit 已经在其他unit上设置,那么启动前者将停止后者,反之亦然。
表1:Unit Option
> 在大多数情况下,仅使用`After`和`Before` unit 文件选项设置排序依赖关系就足够了。 如果还使用 `Wants`(推荐)或`Requires`设置需求依赖关系,则仍需要指定排序依赖关系。 这是因为排序和需求依赖性彼此独立地工作。

使用 man 查看[Unit]查看完整的选项:

$ man systemd.unit

Important Section Options[Install]

Option Description
Alias 空格分隔的 unit 附加名称列表。大多数命令(不包括systemctl systemctl enable)可以使用别名而不是实际的 unit 名称
RequiredBy, WantedBy 当列出的服务被启动,此服务也会被启动。 参考Wants Requires [Unit]
Also 指定用户运行systemctl enable systemctl disable时要与此 unit 一起启用或禁用的 unit 列表

使用 man 查看[Install]查看完整的选项:

$ man systemd.unit

Important Section Options[Service]

Option Description
Type 配置影响ExecStart功能和相关选项的 unit 进程启动类型。
+ simple - 默认值。从ExecStart开始的进程是服务的主要进程
+ forking - 从ExecStart开始的进程产生了一个子进程,该进程成为该服务的主要进程。启动完成后,父进程退出。
+ oneshot - 类似于simple, 但进程在启动后续units 之前会退出
+ dbus - 类似于simple,但只有在主进程获得D-Bus名称后才启动后续unit
+ notify - 类似于simple,但只有在通过 sd_notify() 函数发送通知消息后才会启动后续unit
+ idle - 类似于simple,服务二进制文件的实际执行被延迟,直到所有作业完成,这避免了将状态输出与服务的shell输出混合。
ExecStart 指定启动设备时要执行的命令或脚本。ExecStartPreExecStartPost指定要在ExecStart之前和之后执行的自定义命令。Type=oneshot可以指定多个自定义命令,然后按顺序执行。
ExecStop 指定 unit 停止时要执行的命令或脚本。
ExecReload 指定重新加载 unit 时要执行的命令或脚本
Restart 启用此选项后,服务将在其进程退出后重新启动,但systemctl命令将执行干净停止( clean stop )
RemainAfterExit 如果设置为True,即使退出所有进程,该服务也会被视为活动状态。默认值为False。如果配置了Type=oneshot,则此选项特别有用

使用 man 查看[Service]查看完整的选项:

$ man systemd.service

postfix.service Unit File 例子

下面是 postfix 包的 systemd 服务文件(/usr/lib/systemd/system/postfix.service redhat OS):

[Unit]
Description=Postfix Mail Transport Agent
After=syslog.target network.target
Conflicts=sendmail.service exim.service

[Service]
Type=forking
PIDFile=/var/spool/postfix/pid/master.pid
EnvironmentFile=-/etc/sysconfig/network
ExecStartPre=-/usr/libexec/postfix/aliasesdb
ExecStartPre=-/usr/libexec/postfix/chroot-update
ExecStart=/usr/sbin/postfix start
ExecReload=/usr/sbin/postfix reload
ExecStop=/usr/sbin/postfix stop

[Install]
WantedBy=multi-user.target

更多信息,参考 arch 文档:systemd

原文地址:https://www.cnblogs.com/gardenofhu/p/11497659.html