-1

I have a server that sometimes stop working, the only solution i found is to create a cron that execute this command:

netstat -tn | grep -c :80

The following command will output the number of connections to the port 80. The problem is that i want that if the output is 0 then the server needs to reboot.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Solved, with some searches i wrote this:

#!/bin/bash
while true
do
OUTPUT=`netstat -tn | grep -c :80`
if [ "$OUTPUT" == '0' ]; then
echo 'No connections, rebooting :)'
else
echo "$OUTPUT"
fi
sleep 1
done 
  • 1
    You are rebooting a host if it has no incoming and outgoing TCP port 80 connections within the last 2 minutes? Are you sure that you are doing the right thing? And the bash script with the while loop and the "sleep 1" shows that you still have a lot to learn about unix. – reichhart Jun 24 '17 at 03:26
  • 1
    Actually the command you are searching for is " netstat -nt|awk '{print$4}'|grep -c ':80$' " (checks local port). But you think about why noone answered your question (except yourself) and why I was asking the questions. You are going the wrong way. Don't use this command, don't use a cronjob for your issue and don't use an endlessly running script. Think about the real issue, ask the matching question and then you will get a perfect answer here. – reichhart Jun 24 '17 at 07:19
  • @reichhart Right. You sure about that command though? When I just tried netsat -nt the 4th field is the state so piping that to grep for a port number will never find it. I think what you probably meant was `netstat -nt | awk '$3~/:80$/{c++} END{print c+0}'` (no grep required of course) but I don't use netsat so I'm just guessing at the right field number. – Ed Morton Jun 25 '17 at 12:49
  • 1.) I just fixed bugs in ewwew's command. Using 2 instead of 3 commands like your solution is fine. 2.) On my Linux system 4 is local address and state is 6. – reichhart Jun 26 '17 at 06:18