我需要模拟 Upstart 的“停止时启动”行为,其中服务 B 在服务 A 运行完成后启动,但在 systemd 中。我怎么做?
我在文件的部分中看到了对"After="and"Before="子句的引用,但它们似乎导致服务 B 在服务 A 已启动后启动。同样,在启动服务 B 之前,我需要等到服务 A运行完成。[Unit]*.service
我整理了一个简单的例子来处理这种行为。我把我的*.service文件放在/etc/systemd/system,启用这两个服务,然后重新启动。我希望first.sh在second.sh“sleep for 2 seconds ”之前看到“...and we're out” ,但我没有得到那个结果,你将在下面看到。
我很感激你的指导。
====
这是我的服务文件、它们调用的脚本和 journalctl 输出。
这是“first.service”:
[Unit]
Description=First of two services
[Service]
ExecStart=/home/steve/play/systemd/oneAfterTheOther/first.sh
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
这是“first.sh”:
#!/usr/bin/env bash
nsec=10
echo "sleep for ${nsec} seconds"
sleep ${nsec}
echo "...and we're out"
Run Code Online (Sandbox Code Playgroud)
这是“second.service”:
[Unit]
Description=Second of two services
After=first.service
[Service]
ExecStart=/home/steve/play/systemd/oneAfterTheOther/second.sh
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
这是“second.sh”:
#!/usr/bin/env bash
nsec=2
echo "sleep for ${nsec} seconds"
sleep ${nsec}
echo "...and we're out"
Run Code Online (Sandbox Code Playgroud)
最后,这里是 journalctl 输出:
$ journalctl -u first -u second
-- Logs begin at Tue 2018-09-04 17:50:19 CDT, end at Tue 2018-09-04 17:56:37 CDT
Sep 04 17:50:38 sk-xenial-vm systemd[1]: Started First of two services.
Sep 04 17:50:38 sk-xenial-vm systemd[1]: Started Second of two services.
Sep 04 17:50:40 sk-xenial-vm first.sh[900]: sleep for 10 seconds
Sep 04 17:50:40 sk-xenial-vm second.sh[924]: sleep for 2 seconds
Sep 04 17:50:43 sk-xenial-vm second.sh[924]: ...and we're out
Sep 04 17:50:51 sk-xenial-vm first.sh[900]: ...and we're out
Run Code Online (Sandbox Code Playgroud)
Ste*_*ier 14
我已经找到了我自己问题的答案。
我在systemd.service 手册页中阅读了有关“ Type=”子句的内容,我看到当我将“ Type=oneshot”添加到[Service]我的*.service文件部分时,systemd 会执行我想要的操作。
我希望我可以使用其他Type=设置并获得我喜欢的结果,而且我也希望我实际上可能不需要进行second.serviceoneshot 来获得我想要的结果;first.service是我需要看到完成的。但我现在有一条通往成功的道路。
所以,作为记录,这里有工作 *.service 文件和 journalctl 输出来证明它。
向前!
====
一、服务:
[Unit]
Description=First of two services
[Service]
ExecStart=/home/steve/play/systemd/oneAfterTheOther/first.sh
Type=oneshot
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
二、服务:
[Unit]
Description=Second of two services
After=first.service
[Service]
ExecStart=/home/steve/play/systemd/oneAfterTheOther/second.sh
Type=oneshot
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
Journalctl 输出:
$ journalctl -u first -u second
-- Logs begin at Wed 2018-09-05 11:46:04 CDT, end at Wed 2018-09-05 11:51:54 CDT
Sep 05 11:46:21 sk-xenial-vm systemd[1]: Starting First of two services...
Sep 05 11:46:22 sk-xenial-vm first.sh[868]: sleep for 10 seconds
Sep 05 11:46:32 sk-xenial-vm first.sh[868]: ...and we're out
Sep 05 11:46:32 sk-xenial-vm systemd[1]: Started First of two services.
Sep 05 11:46:32 sk-xenial-vm systemd[1]: Starting Second of two services...
Sep 05 11:46:32 sk-xenial-vm second.sh[1104]: sleep for 2 seconds
Sep 05 11:46:34 sk-xenial-vm second.sh[1104]: ...and we're out
Sep 05 11:46:34 sk-xenial-vm systemd[1]: Started Second of two services.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23317 次 |
| 最近记录: |