0

When you create two or more custom systemd services, how do you know which order they will be run? And will that order be sure thing?

I am doing Type = idle which is supposed to mean

Behavior of idle is very similar to simple; however, actual execution of the service program is delayed until all active jobs are dispatched. This may be used to avoid interleaving of output of shell services with the status output on the console. Note that this type is useful only to improve console output, it is not useful as a general unit ordering tool, and the effect of this service type is subject to a 5s timeout, after which the service program is invoked anyway

Below is my /etc/systemd/system/myservice1.service

#!/bin/bash

[Unit]
Description=my service
After=default.target

[Service]
Type=idle
ExecStart=/root/my_service1.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

I then do

{ create /root/my_service.sh }

chmod 700 /root/my_service1.sh

systemctl daemon-reload

systemctl enable my_service1

If I create many services of type idle, how do I know which order these services will be run in? I do want them to only happen after the system has fully booted and everything boot related has finished.

I then plan on having a few custom do_this.service and do_that.service. I have not yet decided how I am naming everything.

2

The attributes Before and After, to be included in the [Unit] section of your service, should allow you to also impose an order between different systemd services, and not only a service and the "default" target (see e.g. here for more information).

So, if you want myservice1.service to be started before myservice2.service, it should be sufficient to add

Before=myservice2.service

to your service definition file, and reasonably also

After=myservice1.service

to that of myservice2.service.

1
  • 1
    And this behavior isn't limited to idle services. Systemd is opportunistic and nondeterministic by default. Any systemd task which has been brought into the graph (shows up as a symlink in a wants or requires directory) and which has had it's antecedents completed may run at any time and in whichever order systemd decides is most convenient. If you need ordering tell systemd about it. Just don't overdo it: if systemd finds an ordering cycle systemd will break it by killing a random member of the cycle. – davolfman Nov 12 '19 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.