121

I have a general question. How does one start a systemd unit *.service after a particular *.service has started successfully?

More specific question is, how do I start website.service only after mongodb.service has started? In other words website.service should depend on mongodb.service.

5
  • 2
    This question belongs to ServerFault, no ?
    – Rémi
    Commented Feb 21, 2014 at 9:20
  • 1
    @Rémi it's okay on both sites! :) Commented Jul 12, 2016 at 4:08
  • 4
    @AnonymousPenguin No, just it was like 3.5 years before you replied, it's not an on-topic question for SO. It's not about programming. SF or, better, Unix/Linux.SE would be far more appropriate. Commented Oct 9, 2016 at 18:49
  • 1
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.
    – jww
    Commented Jan 28, 2018 at 22:23
  • Possibly relevant ServerFault question: serverfault.com/questions/812492/…
    – Compholio
    Commented Oct 5, 2021 at 20:35

2 Answers 2

170

In the .service file under the [Unit] section:

[Unit]
Description=My Website
After=syslog.target network.target mongodb.service

The important part is the mongodb.service

The manpage describes it however due to formatting it's not as clear on first sight

systemd.unit - well formatted

systemd.unit - not so well formatted

5
  • 1
    Our mongod service starts a very large database. The subsequent service does not start correctly because it seems mongo takes more time to load the database after start. So our 2nd service starts but fails because mongo has started successfully but has not loaded the database. Maybe a delay could help... Commented Sep 10, 2017 at 17:43
  • Is it possible for systemd service to handle following scenerio .Consider multiple services A,B,C,D . If A exists I want B,C,D to start only after A else if A doesnt exist B,C,D may start in any order . Please help
    – achilles
    Commented Apr 4, 2018 at 13:18
  • @achilles Can you create a fake service A on the systems where A is not needed. In that way the dependencies of B, C, D can be kept simple.
    – Walter A
    Commented Apr 30, 2020 at 13:59
  • The proper html documentation is here: freedesktop.org/software/systemd/man/systemd.unit.html
    – istepaniuk
    Commented Feb 19, 2021 at 20:35
  • The "well-formatted" manpage looks quite outdated. Shouldn't it be removed?
    – Pranav
    Commented Jan 5, 2023 at 12:31
64

After= dependency is only effective when service including After= and service included by After= are both scheduled to start as part of your boot up.

Ex:

a.service
[Unit]
After=b.service

This way, if both a.service and b.service are enabled, then systemd will order b.service after a.service.

If I am not misunderstanding, what you are asking is how to start b.service when a.service starts even though b.service is not enabled.

The directive for this is Wants= or Requires= under [Unit].

website.service
[Unit]
Wants=mongodb.service
After=mongodb.service

The difference between Wants= and Requires= is that with Requires=, a failure to start b.service will cause the startup of a.service to fail, whereas with Wants=, a.service will start even if b.service fails. This is explained in detail on the man page of .unit.

3
  • 13
    I believe the truth is the exact opposite of this answer. After=website.service means "execute my unit after website.service !". That's what is explained in the docs : "After= ensures that the configured unit is started after the listed unit finished starting up". I'm assuming "listed units" are units on the right hand side of "After=".
    – Sindarus
    Commented Jun 29, 2018 at 18:22
  • 2
    "This way, if both a.service and b.service are enabled, then systemd will order b.service after a.service. " Don't you mean the other way around? swap(a, b) Commented Aug 2, 2019 at 10:53
  • @Sindarus In the same link you posted is proof this answer is correct. After= only comes into play when the services are started at the same time (such as bootup) otherwise After= is meaningless. Add Wants= or Requires= to auto start another process. Note that those settings are independent of and orthogonal to the requirement dependencies as configured by Requires=, Wants=, Requisite=, or BindsTo=. It is a common pattern to include a unit name in both the After= and Wants= options, in which case the unit listed will be started before the unit that is configured with these options.
    – RcoderNY
    Commented Sep 13, 2022 at 3:35