Instruct to execute an unit after completing another unit successfully

systemdsystemd-timer

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.

Best Answer

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.

Related Question