112

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.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    This question belongs to ServerFault, no ? – Rémi Feb 21 '14 at 09:20
  • 1
    @Rémi it's okay on both sites! :) – Anonymous Penguin Jul 12 '16 at 04:08
  • 2
    @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. – underscore_d Oct 09 '16 at 18:49
  • 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](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jan 28 '18 at 22:23
  • Possibly relevant ServerFault question: https://serverfault.com/questions/812492/systemd-automatically-start-restart-specific-systemd-service-after-another-ser – Compholio Oct 05 '21 at 20:35

2 Answers2

158

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

LW001
  • 2,452
  • 6
  • 27
  • 36
  • 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... – wayofthefuture Sep 10 '17 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 Apr 04 '18 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 Apr 30 '20 at 13:59
  • The proper html documentation is here: https://www.freedesktop.org/software/systemd/man/systemd.unit.html – istepaniuk Feb 19 '21 at 20:35
  • The "well-formatted" manpage looks quite outdated. Shouldn't it be removed? – Pranav Jan 05 '23 at 12:31
63

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.

ki9
  • 5,183
  • 5
  • 37
  • 48
Umut
  • 2,317
  • 1
  • 17
  • 19
  • 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"](https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Before=). I'm assuming "listed units" are units on the right hand side of "After=". – Sindarus Jun 29 '18 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)` – Mateen Ulhaq Aug 02 '19 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 Sep 13 '22 at 03:35