How to set systemd service dependencies?

vvanscherpenseel asked:

During CentOS 7 system boot nginx start fails with the following error:

2014/08/04 17:27:34 [emerg] 790#0: bind() to a.b.c.d:443 failed (99: Cannot assign requested address)

I suspect this is happening due to the network interfaces not being up yet before attempting to bind to that IP address for serving a vhost over SSL.

My guess is I need to specify the network.service as a requirement for the nginx.service, but I can’t find the network service in /etc/systemd/ at all.

How can I configure the service order or dependencies in systemd?

My answer:


You need, at minimum, After=network.target in the [Unit] section of your unit file, to ensure that the network is up before starting nginx. I have no idea why your unit file doesn’t have it.

Here is a complete example from my handy Fedora system, as shipped by Fedora:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.