如何按顺序启动systemd服务单元?

如何按顺序启动systemd服务单元?

如何让以下服务单元按0-3(0,1,2,3)的顺序启动?我尝试启用 echo-date-1.service 思维,它将需要 echo-date-2,这将需要 echo-date-3,这将需要 echo-date-0,因此首先启动 echo-date-0,然后启动回显日期-1,2,3。当我检查 systemd-analyze 图时,顺序看起来错误:

在此输入图像描述

-------------------
echo-date-1.service
-------------------
[Unit]
Description=Start echo-date-1
Requires=echo-date-2.service
**After=echo-date-0.service**

[Service]
ExecStart=/home/USER/bash/echo-date-1.sh

[Install]
WantedBy=multi-user.target



-------------------
echo-date-2.service
-------------------
[Unit]
Description=Start echo-date-2
Requires=echo-date-3.service
**After=echo-date-1.service**

[Service]
ExecStart=/home/USER/bash/echo-date-2.sh

[Install]
WantedBy=multi-user.target



-------------------
echo-date-3.service
-------------------
[Unit]
Description=Start echo-date-3
Requires=echo-date-0.service
**After=echo-date-2.service**

[Service]
ExecStart=/home/USER/bash/echo-date-3.sh

[Install]
WantedBy=multi-user.target




-------------------
echo-date-0.service
-------------------
[Unit]
Description=Start echo-date-0

[Service]
ExecStart=/home/USER/bash/echo-date-0.sh

[Install]
WantedBy=multi-user.target

编辑: 我相信我已经成功了。我必须同时使用两者需要在里面单元服务文件的部分。仅使用需要或者没用;我必须同时使用两者。是否有一个原因?

这是 systemd-analyze 绘图输出和服务文件的 systemctl 状态(参见 PID 号)

在此输入图像描述

● echo-date-0.service - Start echo-date-0
   Loaded: loaded (/etc/systemd/system/echo-date-0.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2017-07-18 16:04:43 EDT; 9min ago
  Process: 281 ExecStart=/home/USER/bash/echo-date-0.sh (code=exited, status=0/SUCCESS)
 Main PID: 281 (code=exited, status=0/SUCCESS)

Jul 18 16:04:43 localhost systemd[1]: Started Start echo-date-0.

● echo-date-1.service - Start echo-date-1
   Loaded: loaded (/etc/systemd/system/echo-date-1.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2017-07-18 16:04:43 EDT; 10min ago
  Process: 283 ExecStart=/home/USER/bash/echo-date-1.sh (code=exited, status=0/SUCCESS)
 Main PID: 283 (code=exited, status=0/SUCCESS)

Jul 18 16:04:43 localhost systemd[1]: Started Start echo-date-1.

● echo-date-2.service - Start echo-date-2
   Loaded: loaded (/etc/systemd/system/echo-date-2.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2017-07-18 16:04:43 EDT; 10min ago
  Process: 284 ExecStart=/home/USER/bash/echo-date-2.sh (code=exited, status=0/SUCCESS)
 Main PID: 284 (code=exited, status=0/SUCCESS)

Jul 18 16:04:43 localhost systemd[1]: Started Start echo-date-2.

● echo-date-3.service - Start echo-date-3
   Loaded: loaded (/etc/systemd/system/echo-date-3.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2017-07-18 16:04:43 EDT; 10min ago
  Process: 285 ExecStart=/home/USER/bash/echo-date-3.sh (code=exited, status=0/SUCCESS)
 Main PID: 285 (code=exited, status=0/SUCCESS)

Jul 18 16:04:43 localhost systemd[1]: Started Start echo-date-3.

答案1

在里面系统单元文档说,如果你想控制顺序,你必须使用Requires/Before的组合After。在您的示例中,我将设置以下内容:

# echo-date-0.service
[Unit]
Description=Start echo-date-0

[Service]
ExecStart=/home/USER/bash/echo-date-0.sh

[Install]
WantedBy=multi-user.target

# echo-date-1.service
[Unit]
Description=Start echo-date-1
Requires=echo-date-0.service
After=echo-date-0.service

[Service]
ExecStart=/home/USER/bash/echo-date-1.sh

[Install]
WantedBy=multi-user.target

# echo-date-2.service
[Unit]
Description=Start echo-date-2
Requires=echo-date-1.service
After=echo-date-1.service

[Service]
ExecStart=/home/USER/bash/echo-date-2.sh

[Install]
WantedBy=multi-user.target

# echo-date-3.service
[Unit]
Description=Start echo-date-3
Requires=echo-date-2.service
After=echo-date-2.service

[Service]
ExecStart=/home/USER/bash/echo-date-3.sh

[Install]
WantedBy=multi-user.target

然后通过启用echo-date-3.service它将启动所有其他服务。

相关内容