5

I use cloud-config to install and configure DCOS cluster.

Normally agentinstall.service service takes 5 minutes to complete.

Is it possible to instruct to systemd to execute agentconfigure.service only after agentinstall.service completed?

#cloud-config
coreos:
  units:
    - name: "agentinstall.service"
      command: "start"
      content: |
        [Unit]
        Description=agent_setup
        After=network.target

        [Service]
        Type=simple
        User=root
        WorkingDirectory=/tmp
        ExecStartPre=/bin/curl -o /tmp/dcos_install.sh  http://bootstapnode-0.dev.myztro.internal:9090/dcos_install.sh
        ExecStartPre=/bin/chmod 755 dcos_install.sh
        ExecStart=/bin/bash dcos_install.sh slave

        [Install]
        WantedBy=multi-user.target
    - name: "agentconfigure.service"
      command: "start"
      content: |
        [Unit]
        Description=agent_config
        After=agentinstall.service

        [Service]
        Type=simple
        User=root
        WorkingDirectory=/opt/mesosphere/etc/
        ExecStartPre=/bin/echo "MESOS_ATTRIBUTES=cluster:uploader" >> /opt/mesosphere/etc/mesos-slave-common
        ExecStartPre=/bin/rm -f /var/lib/mesos/slave/meta/slaves/latest
        ExecStart=/usr/bin/systemctl restart dcos-mesos-slave

        [Install]
        WantedBy=multi-user.target

Thank you.

3
  • Does using the "After" directive not work? Commented Oct 2, 2017 at 10:06
  • 1
    No , It did not work. I assume it is because After= do not guarantee that ""agentinstall.service"" has completed.
    – UtpMahesh
    Commented Oct 2, 2017 at 13:20
  • Does systemd show the agentinstall service running when it hasn't actually completed the start up? Commented Oct 2, 2017 at 13:31

3 Answers 3

3

I am not aware of a way to get this done with systemd, as I think it is only concerned about starting and running of units. As in, you can use After= to force a unit to start only AFTER the specified one has started, or both start in parallel.

Wants= will cause the specified units to be started in parallel (if the wanted units are not yet started/active), not what you want.

Requires= will, if used in conjunction with After= on agentconfigure.service, ensure that agentconfigure.service is started AFTER agentinstall.service is active ("busy"). Now, you could have a loop in agentconfigure.service that waits, say max 5 minutes, and regularly checks for agentinstall.service to complete before it proceeds with its actual work. The only option I see for this.

EDIT: Another option I think is even better ... agentinstall.service starts agentconfigure.service when installation succeeds, then exits.

1

You want to run after the /bin/bash dcos_install.sh slave command?

Change the service with that command to Type=oneshot. Also then I don't think you need to use ExecStartPre=, just use ExecStart= for everything.


@JdeBP asks me to point out that your echo command is attempting to using shell redirection which will not work.

1
  • Beaten to it. I was going to suggest the same. You might want to add to this answer that running systemctl within a service is generally a bad idea; that {{ExecStart}} values are not shell script and shell script syntax does not work in them; and that there's probably a need for two oneshots here or maybe some tmpfiles stuff.
    – JdeBP
    Commented Oct 4, 2017 at 8:12
0

systemd 249, released in July 2021, added support for new dependency type OnSuccess, which does exactly that - allows you to specify what units should be run after this unit completes.

So if your host has systemd 249 or newer, agentinstall.service needs:

[Unit]
Description=agent_setup
After=network.target
OnSuccess=agentconfigure.service

Now you can do systemctl start agentinstall.service, and whenever it finishes, agentconfigure.service will be started automatically.


For completion - there's also OnFailure= dependency which does the same, but triggers only when unit has failed. It has been introduced in systemd v201, released sometime back in 2013.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .