Windows script to Bash conversion

atorex
atorex used Ask the Experts™
on
I'm convering a Windows sceript in to bash, due to my limitations in bash scripting I need some help in converting the following. the below is checking if Tomcat Web server is running and if not wayting till it is, I could do netstat and grep port 8080 but cant get the rest of the loop.

this is the windows script

  :: Test to see if WebServer is running before starting PA.
  :startWebServerStat
  netstat -na | find "0.0.0.0:8080" > WebServerStat.out

  set WebServerStat=
  for /f %%e in (WebServerStat.out) do set WebServerStat=%%e
  del /q/f WebServerStat.out

  if not defined WebServerStat (
    echo.
    echo Webserver is starting please wait...
    echo Waiting 5 seconds before next check.
    for /l %%r in (1,1,5) do @ping 127.0.0.1 -n 2 -w 1000 > nul
    goto startWebServerStat

I would apreciate any help,
Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Ping will not check the services. Check out

#!/bin/bash
# Script to check important ports on remote webserver
# Copyright (c) 2009 blogama.org
# This script is licensed under GNU GPL version 2.0 or above
# ---------------------------------------------------------------------
 
### This script does a verification on port 25, 53, 80 and 443 ###
### After 2 failed check it will send a mail notification ###
 
######To be modified######
WORKDIR="/root"
###HTTP###
HTTPSERVERIP="192.168.1.106"
HTTPSERVERPORT="80"
##########
###HTTPS###
HTTPSSERVERIP="192.168.1.106"
HTTPSSERVERPORT="443"
##########
###MAIL###
SMTPSERVERIP="192.168.1.106"
SMTPSERVERPORT="25"
##########
###DNS###
DNSSERVERIP="192.168.1.106"
DOMAINTOCHECKDNS="example.com"
ANSWERIP="192.168.1.106"
#########
###NOTIFICATIONS###
EMAIL="admin@example.com"
##########
######End to be modified######
 
 
######Do not make modifications below######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Change dir###
cd $WORKDIR
###Restore when problem fix###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
	exit 1;
  fi
fi
###Check if already notified###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Test SMTP###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_smtp.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_smtp.txt ]; then #Second time, send notification below
    echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_smtp.txt
  else #First notification
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Test HTTP###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_http.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_http.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_http.txt ]; then #Second time, send notification below
    echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_http.txt
  else #First notification
    > server_problem_first_time_http.txt
  fi
fi
 
###Test HTTPS###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_https.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_https.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_https.txt ]; then #Second time, send notification below
    echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_https.txt
  else #First notification
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Test DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_dns.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_dns.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_dns.txt ]; then #Second time, send notification below
    echo "DNS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_dns.txt
  else #First notification
    > server_problem_first_time_dns.txt
  fi
fi
 
###Send mail notification after 2 failed check###
if [ -f server_problem.txt ]; then
  $MAIL -s "Server problem" $EMAIL < /root/server_problem.txt
fi

Open in new window

Code Courtesy: http://www.howtoforge.com/simple-bash-script-to-monitor-your-webserver-remotely-on-different-ports
Top Expert 2007
Commented:
#!/bin/bash
while true
do
   netstat -an | grep -q "0.0.0.0:80" && exit
   echo
   echo Webserver is starting please wait...
   echo Waiting 5 seconds before next check.
   sleep 5
done

Open in new window


You may need to change the string to match depending on how your webserver is configured.

Author

Commented:
Thanks for the help, as always!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial