Centos

如何設置 systemd 服務依賴項?

  • May 17, 2017

在 CentOS 7 系統啟動期間,nginx 啟動失敗並出現以下錯誤:

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?

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

引用自:https://serverfault.com/questions/617823