Score:0

After= option not working in systemd service file

in flag

I have two shell scripts name first.sh and second.sh. The contents of first.sh and second.sh are given below:

    >>first.sh
    #!/bin/bash
    while true
    do 
        echo The current time is $(date)
        sleep(1)
    done



    >>second.sh
    #!/bin/bash
    while true
    do 
        echo The current time from second script is $(date)
        sleep(1)
    done

Similarly, the content of first.service is:

[Unit]
Description=first service
After=second.service
[Service]
ExecStart=/home/abanstola/first.sh

The content of second.service is:

[Unit]
Description=Second service
[Service]
ExecStart=/home/abanstola/second.sh

Note the After=second.service in the first.service code. According to the docs, the first service should not be running until the second service starts. But even if I disable or stop the second service, the first service runs without any problem. What am I doing wrong here?

Score:0
in flag

You misread the documentation. Quote:

If unit foo.service contains the setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service has finished starting up. After= is the inverse of Before=

Highlight by me. This only has an effect if both units are being started. If you disable one of them, it has no effect.

It's not quite clear what you actually want.

Probably better choices are:

Requires=

Similar to Wants=, but declares a stronger requirement dependency. Dependencies of this type may also be configured by adding a symlink to a .requires/ directory accompanying the unit file.

If this unit gets activated, the units listed will be activated as well. If one of the other units fails to activate, and an ordering dependency After= on the failing unit is set, this unit will not be started. Besides, with or without specifying After=, this unit will be stopped if one of the other units is explicitly stopped.

PartOf=

Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.

Ajay Banstola avatar
in flag
Okay so it has no effect if i start them one after another? I don't want to achieve anything specific. I just wanted to look at the effect of "After=". Thanks for the clarification.
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.