nginx服务重启/停止失败

想要重启nginx服务报错

[root@localhost ~]# systemctl restart nginx

Job for nginx.service failed because the control process exited with error code. 
See "systemctl status nginx.service" and "journalctl -xe" for details.

查看详细信息如下

[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 三 2020-10-21 11:09:28 CST; 14s ago
  Process: 4480 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 4477 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 4475 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

1021 11:09:27 localhost.localdomain nginx[4480]: nginx: [emerg] bind() to [::]:80 failed (98...e)
1021 11:09:27 localhost.localdomain nginx[4480]: nginx: [emerg] bind() to 0.0.0.0:80 failed ...e)
1021 11:09:27 localhost.localdomain nginx[4480]: nginx: [emerg] bind() to [::]:80 failed (98...e)
1021 11:09:28 localhost.localdomain nginx[4480]: nginx: [emerg] bind() to 0.0.0.0:80 failed ...e)
1021 11:09:28 localhost.localdomain nginx[4480]: nginx: [emerg] bind() to [::]:80 failed (98...e)
1021 11:09:28 localhost.localdomain systemd[1]: nginx.service: control process exited, code=...=1
1021 11:09:28 localhost.localdomain nginx[4480]: nginx: [emerg] still could not bind()
1021 11:09:28 localhost.localdomain systemd[1]: Failed to start The nginx HTTP and reverse p...r.
1021 11:09:28 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
1021 11:09:28 localhost.localdomain systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

使用 netstat -lntup查看你的后台进程

[root@localhost ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4023/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1635/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1100/cupsd
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      3387/sshd: root@pts
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::80                   :::*                    LISTEN      4023/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1635/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      1100/cupsd
tcp6       0      0 ::1:6010                :::*                    LISTEN      3387/sshd: root@pts
udp        0      0 0.0.0.0:60571           0.0.0.0:*                           667/avahi-daemon: r
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           667/avahi-daemon: r
udp        0      0 0.0.0.0:36389           0.0.0.0:*                           848/dhclient
udp        0      0 0.0.0.0:24251           0.0.0.0:*                           847/dhclient
udp        0      0 0.0.0.0:68              0.0.0.0:*                           848/dhclient
udp        0      0 0.0.0.0:68              0.0.0.0:*                           847/dhclient
udp6       0      0 :::19987                :::*                                848/dhclient
udp6       0      0 :::36389                :::*                                847/dhclient

使用kill -9 干掉进程后再次重启

kill -9 4023
[root@localhost ~]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

重启失败再次查看后台进程

[root@localhost ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4024/nginx: worker
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1635/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1100/cupsd
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      3387/sshd: root@pts
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::80                   :::*                    LISTEN      4024/nginx: worker
tcp6       0      0 :::22                   :::*                    LISTEN      1635/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      1100/cupsd
tcp6       0      0 ::1:6010                :::*                    LISTEN      3387/sshd: root@pts
udp        0      0 0.0.0.0:60571           0.0.0.0:*                           667/avahi-daemon: r
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           667/avahi-daemon: r
udp        0      0 0.0.0.0:36389           0.0.0.0:*                           848/dhclient
udp        0      0 0.0.0.0:24251           0.0.0.0:*                           847/dhclient
udp        0      0 0.0.0.0:68              0.0.0.0:*                           848/dhclient
udp        0      0 0.0.0.0:68              0.0.0.0:*                           847/dhclient
udp6       0      0 :::19987                :::*                                848/dhclient
udp6       0      0 :::36389                :::*                                847/dhclient

进程还是存在不过对应的进程id+1了,也就是之前的nginx不存在但是又启动了一次所以进程id+1了

错误原因

此时的nginx服务,使用了nginx命令启动,而并非传统的systemctl工具执行,所以此时如果要停止nginx服务要使用nginx -s stop命令去执行,而不是systemctl stop nginx命令

[root@localhost ~]# nginx -s stop
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

这里使用nginx -s stop又发生一次报错,说/run/nginx.pid不存在,因为我们之前kill进程的时候改变了参数导致进程不知道指定的配置文件是谁?解决方法:nginx -c /etc/nginx/nginx.conf,再次指定配置文件

[root@localhost ~]# nginx -s stop
//这个时候在停止服务就不会报错了

如果启动nginx服务是使用命令行模式启动的,用systemctl工具是关闭不了的,但是使用systemctl工具启动的,nginx命令行是可以关闭的,也就是nginx命令行的优先级高于systemctl工具的优先级,为了以后的方便管理建议使用nginx命令行模式来管理nginx服务。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值