Search code examples
systemd

Why use ExecStart= (with no value) before another ExecStart=/new/value in a systemd override?


I would like to go the systemd override way to let dockerd to listen to port 2376. So I followed this instruction.

On the other hand, I would like to dig into systemd to know what's going on under the hood. So I tried to inspect the unit file of docker by this command: systemctl cat docker.service

According to the output of the command, two files are involved.

  1. /lib/systemd/system/docker.service
  2. /etc/systemd/system/docker.service.d/override.conf

I believe the first one is default unit file for docker and the second one is the I created.

My problem is: Both files include sentances - ExecStart= and twice in the second file like:

ExecStart=
ExecStart=/usr/bin/dockerd -H fd://

Is it necessary to assign empty to ExecStart= before setting meaningful value ExecStart=/usr/bin/dockerd -H fd:// ?

I have spilit this post into two questions and the other one here.


Solution

  • When you add entries to an override file, they are by default appended to any existing entries. That is, if your service example.service has:

    [Service]
    EnvironmentFile=/etc/foo.env
    

    And you create /etc/systemd/system/example.service.d/override.conf with:

    [Service]
    EnvironmentFile=/etc/bar.env
    

    Then the effective configuration is:

    [Service]
    EnvironmentFile=/etc/foo.env
    EnvironmentFile=/etc/bar.env
    

    That's fine for many directives, but a service can have only one ExecStart (unless it's a Type-oneshot service), so if you try to create an override file like this:

    [Service]
    ExecStart=/new/command/line
    

    That will fail with an error along the lines of:

    systemd: example.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
    

    By specifying an empty ExecStart, you are "clearing out" all previous entries. So if your example.service has:

    [Service]
    ExecStart=/bin/foo
    

    And you create an override like:

    [Service]
    ExecStart=
    ExecStart=/bin/bar
    

    The effective configuration is:

    [Service]
    ExecStart=/bin/bar