Systemd Declaring a Service Dependency: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 12: Line 12:


==Create a httpd systemd service configuration directory==
==Create a httpd systemd service configuration directory==
This is only necessary if the unit file (ex: <code>/usr/lib/systemd/system/httpd.service</code>) does not already exits.


If it does not exist, create a directory:
<font size=-2>
<font size=-2>
/etc/systemd/system/httpd.service.d
/etc/systemd/system/httpd.service.d
</font>
</font>


Line 26: Line 28:
After=network-online.target
After=network-online.target
</syntaxhighlight>
</syntaxhighlight>
⚠️ Wrong configurations used <code>network.target</code>. This is incorrect, it has to be <code>network-online.target</code>.


For more details on the structure of a systemd unit file, see [[systemd Concepts#.5BUnit.5D_Section|the [Unit] section of a unit file]].
For more details on the structure of a systemd unit file, see [[systemd Concepts#.5BUnit.5D_Section|the [Unit] section of a unit file]].
Line 31: Line 35:
=Procedure 2=
=Procedure 2=


<font color=red>TODO: Document this, it worked expressing the haproxy dependency on named.</font>
<font color=darkkhaki>TODO: Document this, it worked expressing the haproxy dependency on named.</font>


Declare the dependency directly on a service, in the dependent service unit file:
Declare the dependency directly on a service, in the dependent service unit file:
 
<syntaxhighlight lang='ini'>
...
...
Requires=named.service
Requires=named.service
After=named.service
After=named.service
...
...
</syntaxhighlight>


This also worked:
This also worked:
 
<syntaxhighlight lang='ini'>
...
...
After=network-online.target sshd-keygen.service
After=network-online.target sshd-keygen.service
Wants=sshd-keygen.service
Wants=sshd-keygen.service
...
...
</syntaxhighlight>

Latest revision as of 03:13, 1 January 2024

Internal

Overview

This example shows how to declare a dependency between httpd and network services - the system should not attempt to start httpd unless the network service are started, otherwise httpd won't find the network interfaces to bind to. For more details on concepts behind dependencies, see:

systemd dependencies

Procedure

Create a httpd systemd service configuration directory

This is only necessary if the unit file (ex: /usr/lib/systemd/system/httpd.service) does not already exits.

If it does not exist, create a directory:

/etc/systemd/system/httpd.service.d

Create a httpd systemd service configuration file that expresses the dependency

Create a /etc/systemd/system/httpd.service.d/http-extra.conf file with the following content:

[Unit]
Requires=network-online.target
After=network-online.target

⚠️ Wrong configurations used network.target. This is incorrect, it has to be network-online.target.

For more details on the structure of a systemd unit file, see the [Unit] section of a unit file.

Procedure 2

TODO: Document this, it worked expressing the haproxy dependency on named.

Declare the dependency directly on a service, in the dependent service unit file:

...
Requires=named.service
After=named.service
...

This also worked:

...
After=network-online.target sshd-keygen.service
Wants=sshd-keygen.service
...