keepalived 脚本检测nginx导致的服务端口占用

来源:本站原创 Linux 超过34 views围观 0条评论

keepalived 脚本检测nginx导致的服务端口占用

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`    —这一行脚本错误 应用 —-no-header 所以脚本会一直重启
if [ $A -eq 0 ];then
/usr/sbin/nginx
sleep 2
if [ `ps -C nginx –no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi

脚本的输出含意为
ps -C nginx –no-header |wc -l
统计 nginx进程的数 如果为0则重启nginx服务
加 –no-header 是为了精确计算行数
[root@shtslb01 ~]# ps -C sshd
  PID TTY          TIME CMD    有头
1109 ?        00:00:00 sshd
4428 ?        00:00:00 sshd
[root@shtslb01 ~]# ps -C sshd –no-header
1109 ?        00:00:00 sshd   —无头
4428 ?        00:00:00 sshd

应急处理方式
1 在keepalived 里删除相关脚本
2 systemctl status nginx
3  pkill keepalived
4  systemctl status nginx
5  systemctl restart nginx 报错
6  systemctl restart keepalived
7  systemctl restart nginx 继续报错
8  netstat -ntlp  80端口还存在
9  pkill nginx
10  netstat -ntlp 端口消失
11  systemctl restart nginx 重启成功报错消除

现像1 无法重启
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@shtslb02 script]#

2 端口被占用报错
2017/09/18 09:30:53 [emerg] 5452#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to [::]:80 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to [::]:80 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: bind() to [::]:80 failed (98: Address already in use)
2017/09/18 09:30:53 [emerg] 5452#0: still could not bind()

● nginx.service – The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2017-09-18 09:34:10 CST; 57s ago
  Process: 5924 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 5921 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 5919 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

Sep 18 09:34:09 shtslb02 nginx[5924]: nginx: [emerg] bind() to 0.0.0.0:8080 failed (98: Address already in use)
Sep 18 09:34:09 shtslb02 nginx[5924]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Sep 18 09:34:09 shtslb02 nginx[5924]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Sep 18 09:34:09 shtslb02 nginx[5924]: nginx: [emerg] bind() to 0.0.0.0:8080 failed (98: Address already in use)
Sep 18 09:34:09 shtslb02 nginx[5924]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Sep 18 09:34:10 shtslb02 nginx[5924]: nginx: [emerg] still could not bind()
Sep 18 09:34:10 shtslb02 systemd[1]: nginx.service: control process exited, code=exited status=1
Sep 18 09:34:10 shtslb02 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Sep 18 09:34:10 shtslb02 systemd[1]: Unit nginx.service entered failed state.
Sep 18 09:34:10 shtslb02 systemd[1]: nginx.service failed.

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=3564转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点