如何让一个 systemd 服务在完成后触发另一个 systemd 服务?

dea*_*sin 5 systemd

我有两个进程需要在不同的条件下运行(Service1 必须从特定的 TTY 运行,而 service2 不能在该 TTY 下运行。),因此它们必须从不同的 systemd 服务运行。但我希望他们背靠背跑。所以我想用计时器触发service1,但我希望在service1之后立即触发service2。

有没有一种优雅的方式来实现这一目标?

服务1

[Unit]
Description=blank the cursor

[Service]
StandardInput=tty-force 
StandardOutput=tty 
TTYPath=/dev/tty1 
Type=simple 
ExecStart=tput civis > /dev/tty1
Run Code Online (Sandbox Code Playgroud)

服务2

[Unit]
Description=random wallpaper change script

[Service]
SyslogIdentifier=wallpaper.sh
User=deanresin
Type=simple 
ExecStart=/bin/bash /home/deanresin/scripts/wallpaper.sh
Run Code Online (Sandbox Code Playgroud)

我希望service1在完成后触发service2。

Ruc*_*t88 3

service1完成后触发service2

您可以从 service1 实现此关系,也可以从 service2 实现此关系。

在服务中实现它1

[Unit]
Before=service2.service
Wants=service2.service # Start service2 after this service
Run Code Online (Sandbox Code Playgroud)

从service2来实现

[Unit]
After=service1.service
BindsTo=service1.service # Only starts if service1 exits with success

[Install]
WantedBy=service1.service # Creates a Wants dependency in service1
Run Code Online (Sandbox Code Playgroud)

源systemd单元手册: https://www.freedesktop.org/software/systemd/man/systemd.unit.html