Score:2

How to start a systemd service after completion of another systemd service of type "simple"

br flag

I have seen similar questions but none of them address the situation I am facing. My service A is defined as follows (Note Type=simple).

[Unit]
Description=Sample service A
Before=shutdown.target
After=multi-user.target
Requires=multi-user.target
Conflicts=shutdown.target

[Service]
Type=simple
RemainAfterExit=yes
Restart=no
ExecStart=/etc/rc.d/init.d/script_a start
ExecStop=/etc/rc.d/init.d/script_a stop

[Install]
RequiredBy=random.target

Another service B needs to be run only after the service A has run to completion. I tried the following but it seems the service B is started as soon as service A has started. Service B is not waiting for A to finish.

[Unit]
Description=Sample service B
After=serviceA.service
Requires=serviceA.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/init.d/scriptb start

[Install]
RequiredBy=random.target

How can I make service B wait till A has finished?

EDIT

I also tried putting OnSuccess=serviceB.service in Service A but did not help.

Score:4
im flag

In order to make service B wait until service A has finished, you can add the WantedBy option to service A and the RequiredBy option to service B, with the same target. Here's how you can modify your unit files:

For service A:

Description=Sample service A
Before=shutdown.target
After=multi-user.target
Requires=multi-user.target
Conflicts=shutdown.target

[Service]
Type=simple
RemainAfterExit=yes
Restart=no
ExecStart=/etc/rc.d/init.d/script_a start
ExecStop=/etc/rc.d/init.d/script_a stop

WantedBy=service-b.target

[Install]
RequiredBy=random.target 

For Service B:

Description=Sample service B
After=serviceA.service
Requires=serviceA.service

RequiredBy=service-b.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/init.d/scriptb start

[Install]
RequiredBy=service-b.target

Service A is marked as WantedBy this target and Service B is marked as RequiredBy this target. This means that Service B will only start when the service-b.target is reached, and the service-b.target is only reached when Service A has completed successfully.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.