2016-04-16 8 views
0

ich ein Bash-Programm, das überprüft, ob ein Dämon in einem bestimmten Port arbeitet:Alternative in CentOS 7 für "nc -Z"

nc -z localhost $port > /dev/null 
if [ "$?" != "0" ] 
then 
    echo The server on port $port is not working 
    exit 
fi 

Dieses Programm perfekt in CentOS funktioniert 6. Es scheint jedoch, dass CentOS 7 hat die grundlegende Implementierung nc Befehl geändert (CentOS 6 scheint Netcat und CentOS 7 verwendet ein anderes Ding namens nCAT verwenden) und jetzt die -z Schalter nicht funktioniert:

$ nc -z localhost 8080 
nc: invalid option -- 'z' 

in CentOS zum man nc Seite Suche 7 Ich sehe keine klare Alternative ernative zu -z. Irgendwelche Vorschläge, wie ich mein Bash-Programm reparieren sollte, damit es in CentOS 7 funktioniert?

+0

der Nähe Flag In Bezug auf die ausgelöst wurde, lassen Sie mich klarstellen, dass Die Frage * ist * über die Programmierung (insbesondere Bash-Programmierung). – fgalan

+1

Wenn Sie sich nicht für das Tool interessieren, das Sie verwenden, können Sie wahrscheinlich die Antworten unter http://stackoverflow.com/questions/4922943/test-from-shell-script-i-f-remote-tcp-port-is verwenden -open –

+0

'if [" $? " ! = "0"] 'ist ein Antipattern; du willst einfach 'if nc -z localhost $ port>/dev/null' – tripleee

Antwort

0

ich etwas ähnliches mit dem folgenden Code gelöst:

# Start command: nohup ./check_server.sh 2>&1 & 
# 
FREQUENCY_CHECK=30 
ENDPOINT=10.11.12.13 
PORT=8080 
# Avoid spamming yourself 
EMAIL_LIMIT=3 


check_server(){ # Start shell function 

checkHTTPcode=$(curl -sLf -m 2 -w "%{http_code}\n" "http://$ENDPOINT:$PORT/" -o /dev/null) 


if [ $checkHTTPcode -ne 200 ] 

    then 
     EMAIL_LIMIT=$((EMAIL_LIMIT-1)) 

     # Let's log. Life's boring 
     echo "$(date) Check Failed " >> /var/log/check_server.log 

    if [ $EMAIL_LIMIT -eq 0 ] 
     then 

     # Send last email.. 
     printf "%s\nCheck failed \n\n Last email to be sent. " | mail -s "Check Failed!" -S smtp=your.smtp.domain.here.com -S from="[email protected]" [email protected] 

     else 

      if [ $EMAIL_LIMIT -ge 0 ] 
       then 
       # Send notification 
       printf "%s\nCheck failed \n\n Notifications left: $EMAIL_LIMIT " | mail -s "Check Failed!" -S smtp=your.smtp.domain.here.com -S from="[email protected]" [email protected] 
      fi 

    fi 

     # Take any corrective measure here if nedded. 
     /sbin/service httpd restart >> /var/log/check_server.log 

else 
    # Let's log. Life's boring 
    echo "$(date) Check OK " >> /var/log/check_server.log 
fi 
} 


while true # infinite check 
do 
# Call function every 30 seconds 
check_server 
sleep $FREQUENCY_CHECK 
done 
+0

Danke! Ich werde den "curl" -Befehl unter "checkHTTPcode" sehen, um zu sehen, wie es funktioniert. – fgalan

2

Und eine abgespeckte Version für das, was Sie wirklich wollen:

#!/bin/bash 
# Start command: nohup ./check_server.sh 2>&1 & 

check_server(){ # Start shell function 

checkHTTPcode=$(curl -sLf -m 2 -w "%{http_code}\n" "http://10.10.10.10:8080/" -o /dev/null) 

if [ $checkHTTPcode -ne 200 ] 
    then 
     # Check failed. Do something here and take any corrective measure here if nedded like restarting server 
     # /sbin/service httpd restart >> /var/log/check_server.log 
     echo "$(date) Check Failed " >> /var/log/check_server.log 

    else 
     # Everything's OK. Lets move on. 
     echo "$(date) Check OK " >> /var/log/check_server.log 
fi 
} 

while true # infinite check 
do 
# Call function every 30 seconds 
check_server 
sleep 30 

done