I would like to start my service samplerbox.service

[Unit]
Description=Starts SamplerBox

[Service]
Type=simple
ExecStart=/root/SamplerBox/samplerbox.sh          # this script does: python /root/SamplerBox/samplerbox.py
WorkingDirectory=/root/SamplerBox/

[Install]
WantedBy=multi-user.target

before networking.service (DHCP thing, IP attribution, etc.) has finished.

Remark: my program doesn't use networking at all.


Here is the plot (bigger size here):

http://gget.it/sb5sfgln/ets.svg

As you can see, samplerbox.service exactly waits for networking.service to be finished. (I did several tests, and it is the same).

I tried: replace WantedBy=multi-user.target by something else like WantedBy=sound.target and then systemctl disable samplerbox.service and reenable it... But this was unsuccessful!

How to force a service to start before networking.service?

share|improve this question
    
Here is the result of systemctl show networking – Basj May 30 '15 at 0:55
    
Have you tried WantedBy=networking.service? – Mikel May 30 '15 at 0:59
    
@Mikel: I tried [Install] WantedBy=networking.service but it's the same: gget.it/b4ld73lt/plot4.svg ... See bottom (CTRL+F, then "samplerbox") : samplerbox.service is again started after networking has finished... – Basj May 30 '15 at 9:30
up vote 5 down vote accepted

samplerbox.service is started after networking has finished.

True, but only an incidental and not what is actually happening. If you look carefully at the graph you'll find that samplerbox.service is being started after basic.target. This is normal and by design in systemd. Most (non-system) services have "default dependencies" set, which are implicit dependencies and orderings that are not written out in the service unit. A dependency from basic.target and an ordering after it is one such default that is applied by systemd, unless the defaults are explicitly disabled in the service unit.

You are also conflating ordering and dependency. The various "wants" directives, such as WantedBy that you have been playing with and its reverse Wants, specify dependencies. They make systemd queue a job to also start service B if a request is made to start service A. They do not specify the orderings of those jobs. Orderings are controlled by other settings, namely the fairly obviously named Before and After.

Dependencies specify what overall set of start and stop jobs is constructed from a simple command such as systemctl start graphical.target. Orderings specify when and in what order those jobs are then executed.

Further reading

  • Lennart Poettering (2013-10-07). systemd.unit. systemd manual pages. freedesktop.org.
  • Lennart Poettering (2013-10-07). systemd.service. systemd manual pages. freedesktop.org.
share|improve this answer
    
Thanks @JdeBP for this detailed answer. Now, for a solution : how to force my service to run as soon as possible? If I systemctl disable networking, my service starts @4sec, but if networking is still enabled, mu service starts @10 or 12sec, that's bad. How to force my service to start before everything else? – Basj May 30 '15 at 10:58
1  
Solved with DefaultDependencies=false! Thanks so much for having pointed me to the right docs! – Basj May 30 '15 at 11:43
1  
@Basj: you shouldn't set DefaultDependencies=false unless you precisely know what are you doing. For example, this may make your service start with read-only root filesystem, or before time is set up, or... whatever. Most probably, you actually want to remove the "sysinit.target is after network.target" dependency, it is strange and bogus. Removing this dependency will make all services start earlier and faster. – intelfx May 30 '15 at 21:00
1  
@Basj: find the exact unit file (in /usr/lib/systemd/system) where it is specified. Copy it to /etc/systemd/system under the same name. Edit the copied file, removing the ordering dependency. – intelfx May 31 '15 at 12:46
1  
Debian unstable (sid) has systemd 215 and sysinit.target is indeed after networking.service. OpenSUSE 13.2 has systemd 210 and sysinit.target is not after network.service. Not sure who has changed that and why. – Uwe Geuder Jun 1 '15 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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