我准备了两个服务a.service和b.service。 a.service 使用 a.timer 运行。
a.服务
[Unit]
Description=service a
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
#restart on failure and tries 5 times. If fail in all, then reboot the system
ExecStart=/usr/bin/python /home/pi/.clar/a.py
Restart=on-failure
RestartSec=15
StartLimitBurst=5
StartLimitInterval=2min
StartLimitAction=reboot
a.定时器
[Unit]
Description=10minute timer
[Timer]
# start this 20 Sec after boot:
OnBootSec=20
# ... and then every 10 minute:
OnUnitActiveSec=10m
[Install]
WantedBy=multi-user.target
b.服务
[Unit]
Description=b service
After=a.service
[Service]
Type=simple
#restart on failure and tries 5 times. If fail in all, then reboot the system
ExecStartPre=/usr/bin/python /home/pi/.clar/c.py
ExecStart=/usr/bin/python /home/pi/.clar/b.py
Restart=on-failure
RestartSec=15
StartLimitBurst=5
StartLimitInterval=2min
StartLimitAction=reboot
[Install]
WantedBy=multi-user.target
由于我的配置 a.service 将在启动后 20 秒后启动,而 b.service 应在 a.service 之后运行。
但实际上 b.service 是在 a.service 之前启动的。我搜索了 google,并尝试在 b.service unit 部分中添加Requires=a.service
和。Wants=a.service
但没有人帮助我。
我不明白工作后。配置中还需要添加什么吗?
答案1
正如muru所说,你可以像下面这样修改它。
[Unit]
Description=b service
After=a.service
[Service]
Type=simple
#restart on failure and tries 5 times. If fail in all, then reboot the system
ExecStartPre=/usr/bin/python /home/pi/.clar/c.py
ExecStart=/usr/bin/python /home/pi/.clar/b.py
Restart=on-failure
RestartSec=15
StartLimitBurst=5
StartLimitInterval=2min
StartLimitAction=reboot
[Install]
WantedBy=a.service
然后你必须做sudo systemctl disable b.service
并且sudo systemctl enable b.service