systemd service 运行 其他服务启动前后
systemd service run before and after other service starts
我想运行 autofs 启动前后的脚本。
我有两个系统服务:
backup1.service 运行 秒之前 autofs
[Unit]
Description=Backup mount
[Service]
ExecStart=/backup/sw/bmount before
[Install]
WantedBy=autofs.service
backup2.service 运行 秒后 autofs
[Unit]
Description=Backup mount
PartOf=autofs.service
After=autofs.service
[Service]
ExecStart=/backup/sw/bmount after
[Install]
WantedBy=autofs.service
我可以确定 bmount 脚本中的 after/before 状态,所以我可以不带参数调用它,我只能使用一个服务,但不知道如何。
可能吗?
有几种方法可以做到这一点:
编辑autofs.service
根据设计,服务文件应该是站点可维护的。在基于 Debian 的平台上,供应商提供的服务文件在 /lib/systemd/system/
中,我认为 redhat 在 /usr/lib/systemd/system/
中有它们,但您可以在 /etc/systemd/system/
中用站点管理的服务文件覆盖它们。
在那种情况下,我会
cp /lib/systemd/system/autofs.service /etc/systemd/system/autofs.service
然后在 [Service]
部分,我会添加:
ExecStartPre=/backup/sw/bmount before
ExecStartPost=/backup/sw/bmount after
systemd.service
联机帮助页说:
ExecStart= commands are only run after all ExecStartPre= commands exit successfully.
ExecStartPost= commands are only run after the commands specified in ExecStart= have been invoked successfully, as determined by Type= (i.e. the process has been started for Type=simple or Type=idle, the last ExecStart= process exited successfully for Type=oneshot, ...).
即插即用服务参数
一种更优雅的方式来完成与上述相同的事情,即使用 drop-in。只需使用以下内容创建 /etc/systemd/system/autofs.service.d/backup.conf
:
[Service]
ExecStartPre=/backup/sw/bmount before
ExecStartPost=/backup/sw/bmount after
关系
也许 autofs.service
已经有 ExecStartPre
和 ExecStartPost
命令,您担心会干扰该服务。在这种情况下,您可以使用关系 start/stop 您的服务。
[Unit]
Description=Backup mount
PartOf=autofs.service
Before=autofs.service
[Service]
Type=oneshot
ExecStart=/backup/sw/bmount before
[Install]
WantedBy=autofs.service
和
[Unit]
Description=Backup mount
PartOf=autofs.service
After=autofs.service
[Service]
Type=oneshot
ExecStart=/backup/sw/bmount after
[Install]
WantedBy=autofs.service
在这种情况下:
PartOf=autofs.service
表示"When systemd stops or restarts autofs.service
, the action is propagated to backup.service
"
Before=autofs.service
表示"If both units are being started, autofs.service
's startup is delayed until backup.service
has finished starting up."
After=autofs.service
表示"If both units are being started, backup.service
's startup is delayed until autofs.service
has finished starting up."
WantedBy=autofs.service
表示“如果 autofs.service
是,backup.service
将启动”。
Type=oneshot
表示服务仍将被视为 运行ning,即使在 ExecStart=
进程完成后也是如此。
务必运行 systemctl daemon-reload
以便 systemd 读取新服务。另外 运行 systemctl enable backup.service
以确保 WantedBy=
成为 autofs.service
的 Wants=
。
我认为您的解决方案非常接近。
我想运行 autofs 启动前后的脚本。 我有两个系统服务:
backup1.service 运行 秒之前 autofs
[Unit]
Description=Backup mount
[Service]
ExecStart=/backup/sw/bmount before
[Install]
WantedBy=autofs.service
backup2.service 运行 秒后 autofs
[Unit]
Description=Backup mount
PartOf=autofs.service
After=autofs.service
[Service]
ExecStart=/backup/sw/bmount after
[Install]
WantedBy=autofs.service
我可以确定 bmount 脚本中的 after/before 状态,所以我可以不带参数调用它,我只能使用一个服务,但不知道如何。
可能吗?
有几种方法可以做到这一点:
编辑autofs.service
根据设计,服务文件应该是站点可维护的。在基于 Debian 的平台上,供应商提供的服务文件在 /lib/systemd/system/
中,我认为 redhat 在 /usr/lib/systemd/system/
中有它们,但您可以在 /etc/systemd/system/
中用站点管理的服务文件覆盖它们。
在那种情况下,我会
cp /lib/systemd/system/autofs.service /etc/systemd/system/autofs.service
然后在 [Service]
部分,我会添加:
ExecStartPre=/backup/sw/bmount before
ExecStartPost=/backup/sw/bmount after
systemd.service
联机帮助页说:
ExecStart= commands are only run after all ExecStartPre= commands exit successfully.
ExecStartPost= commands are only run after the commands specified in ExecStart= have been invoked successfully, as determined by Type= (i.e. the process has been started for Type=simple or Type=idle, the last ExecStart= process exited successfully for Type=oneshot, ...).
即插即用服务参数
一种更优雅的方式来完成与上述相同的事情,即使用 drop-in。只需使用以下内容创建 /etc/systemd/system/autofs.service.d/backup.conf
:
[Service]
ExecStartPre=/backup/sw/bmount before
ExecStartPost=/backup/sw/bmount after
关系
也许 autofs.service
已经有 ExecStartPre
和 ExecStartPost
命令,您担心会干扰该服务。在这种情况下,您可以使用关系 start/stop 您的服务。
[Unit]
Description=Backup mount
PartOf=autofs.service
Before=autofs.service
[Service]
Type=oneshot
ExecStart=/backup/sw/bmount before
[Install]
WantedBy=autofs.service
和
[Unit]
Description=Backup mount
PartOf=autofs.service
After=autofs.service
[Service]
Type=oneshot
ExecStart=/backup/sw/bmount after
[Install]
WantedBy=autofs.service
在这种情况下:
PartOf=autofs.service
表示"When systemd stops or restartsautofs.service
, the action is propagated tobackup.service
"Before=autofs.service
表示"If both units are being started,autofs.service
's startup is delayed untilbackup.service
has finished starting up."After=autofs.service
表示"If both units are being started,backup.service
's startup is delayed untilautofs.service
has finished starting up."WantedBy=autofs.service
表示“如果autofs.service
是,backup.service
将启动”。Type=oneshot
表示服务仍将被视为 运行ning,即使在ExecStart=
进程完成后也是如此。
务必运行 systemctl daemon-reload
以便 systemd 读取新服务。另外 运行 systemctl enable backup.service
以确保 WantedBy=
成为 autofs.service
的 Wants=
。
我认为您的解决方案非常接近。