7

How can I get the following service units to start up in order from 0-3 (0,1,2,3)? I tried enabling echo-date-1.service thinking, it will require echo-date-2, which will require echo-date-3, which will require echo-date-0, therefore starting echo-date-0 first, then starting echo-date-1,2,3. When I check systemd-analyze plot, the order looks wrong:

enter image description here

-------------------
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

Edit: I believe I got this working. I had to use both Requires and After in the Unit section of the service file. Using only Requires or After didn't work; I had to use both. Is there a reason for this?

Here is the systemd-analyze plot output and the systemctl status for service files (see PID number)

enter image description here

● 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 Answer 1

12

In the systemd.unit documentation it says that if you want to control the order you have to use a combination of Requires with Before/After. In your example I would set the following:

# 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

And then by enabling echo-date-3.service it would start all other services.

2
  • accepting this as the answer. the Requires+After ordering makes alot more sense then my edit.
    – jes516
    Commented Jul 19, 2017 at 20:35
  • Will this be overwritten after upgrade of related packages? Commented Jul 11, 2020 at 1:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .