keepalived 腳本檢測nginx導致的服務端口占用

來源:本站原創 Linux 超過853 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 是為了精確計算行數
[[email protected] ~]# ps -C sshd
  PID TTY          TIME CMD    有頭
1109 ?        00:00:00 sshd
4428 ?        00:00:00 sshd
[[email protected] ~]# 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.
[[email protected] 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.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=3564轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
?
?
萌宠夺宝游戏