Shell script to check if a port is in netstat then act on it
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Location: Under the bridge where proper engineers walkover
Distribution: Various Linux, Solaris, BSD, Cisco
Posts: 443
Rep:
Shell script to check if a port is in netstat then act on it
Hi,
I am trying to write a shell script which will take in the output of netstat and look for a specific port then determine that the correlating service is up and running or not. Then tie it into a cron job to automate.
What I have so far is this:
Code:
#!/bin/sh
ntstat=`netstat -ap tcp | grep 8180 | sed -n '1p'`
port=".8180*"
#echo $ntstat
#echo $port
if [ $ntstat =~ $port ]; then
echo "Output of Netstat command $ntstat port number $port";
else
wait 60; /usr/local/etc/rc.d/tomcat6 restart;
fi
It doesn't work and is far from working, hence I desperately need to improve my shell scripting capability but for now here's what I want the script to do in step order:
1. run netstat and output the desired port on one line - 8180 (Tomcat6)
2. store the information into a variable
3. compare the result of netstat with the port number 8180
4. conditionally if it's online do nothing
5. if it's not then wait 60 seconds and then restart tomcat
I'm having to build in some intelligence to my system as for whatever reason - probably lack of memory tomcat does segfault or run out of Java Heap Space - which I rectified.
Anyway it's a home server and I'm not in a position to get a memory hike as the machine's system board is maxed out and it's running on Mini-ITX hardware meaning only 4GB max for Core2Quad based system boards.
Well I do not have tomcat running but your netstat seems to behave differently to mine
Here is an example:
Code:
$ netstat -ap tcp | grep 5656 | sed '1p'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
unix 3 [ ] STREAM CONNECTED 5656 -
unix 3 [ ] STREAM CONNECTED 5656 -
The first 2 lines are actually printed to standard error so I just wanted to check with you
This seemed to solve it for me:
Location: Under the bridge where proper engineers walkover
Distribution: Various Linux, Solaris, BSD, Cisco
Posts: 443
Original Poster
Rep:
I adapted the script a little:
Code:
#!/usr/local/bin/bash
ntstat=`netstat -ap tcp | grep 8180 | sed -n '1p'`
port=".8180*"
#echo $ntstat
#echo $port
if [[ $ntstat =~ $port ]]; then
echo "Output of Netstat command $ntstat port number $port" > /root/java_restart/java_restart.log;
else
wait 60; /usr/local/etc/rc.d/tomcat6 restart;
fi
I chose .8180* as I wasn't quite sure if using 8180 would also print out other ports not used by tomcat as this is the general output of my netstat:
Code:
wiki# netstat -ap tcp
netstat: kvm not available: /dev/mem: No such file or directory
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 wiki.8180 proxy.55891 ESTABLISHED
tcp4 0 0 wiki.postgresql wiki.12899 ESTABLISHED
tcp4 0 0 wiki.12899 wiki.postgresql ESTABLISHED
tcp4 0 0 wiki.8005 *.* LISTEN
tcp4 0 0 wiki.8009 *.* LISTEN
tcp4 0 0 wiki.8180 *.* LISTEN
tcp4 0 0 wiki.postgresql *.* LISTEN
tcp4 0 0 wiki.smtp *.* LISTEN
as I don't want to get the Foreign Address portion matched too.
I mean I wanted do the script the forward mentality way to test if the port was not on first then restart and if it was up then to just leave it. However my scripting skills are far from being perfect so I wasn't quite sure how to go about and it seemed easier to test the condition I used in the end.
Location: Under the bridge where proper engineers walkover
Distribution: Various Linux, Solaris, BSD, Cisco
Posts: 443
Original Poster
Rep:
Yeah actually it's strange that Tomcat6 is quite unstable on FreeBSD - am running 8.2 x64 edition.
Which is basically why I need to take extra percautions!
At first I was running out of Java Heap Space which I rectified and after telling the system to use 3GB it finally worked. Having said that I tested the same webapp (Xwiki) in Nexenta Core 3 with GlassFishV3 on a VM in my notebook with only 2GB memory allocated to it.
I ran for weeks and was fine - ok really slow but still.
I also tested on my Fedora 11 PPC based machine with only 384MB RAM which worked fine for a while but eventually swap space started getting colonized as Java needs a lot of memory to run on. But at least that instance even lasted for a week. On the most powerful system I have however (core2quad 4GB RAM), I keep getting problems. I think this should take care of most things though.
I can't simply reformat as the system is a sort'a home mainframe doing everything in one box and the reason behind FreeBSD in the first place was amount of apps readily available in repos plus ZFS file system. Not to mention really low memory footprint over Linux distro's I know plus the 4GB min that Solaris and it's variants need. + I like FreeBSD too :-) it's cool!
What I would do instead is query the process that's started from /usr/local/etc/rc.d/tomcat6 for its process id and check that process for ports it's listening on (or use say 'fuser -n tcp 8180' to isolate). In the end though a kludge that isn't in any way reliable as the port may be bound to but it won't return anything if the process is stuck and the service may not be accessible from remote hosts (OK, if that's required). An easy to install application like Monit has the advantage of 0) being faster than interpreted code, 1) has ready-made checks built in to test using process Id, ICMP, HTTP(S), content strings, content hashes using an easy to understand syntax and 2) can restart processes and alert you if anything occurs...
Waow, core2quad 4GB RAM and have stabilities issues ? Java is evil :/
In some way, I think I would consider testing an Open Solaris install maybe this java app would run smoother in it ? (java is SUN product after all)
(edit)
Just see that OpenSolaris project is discontinued :/
Location: Under the bridge where proper engineers walkover
Distribution: Various Linux, Solaris, BSD, Cisco
Posts: 443
Original Poster
Rep:
Thanks UnSpawn for that but Monit unfortunately takes memory!
I had to disable: Munin, Zabbix, Cacti, and basically any other non-essential service to make room for Tomcat.
The few things I can't disable are Bind, NTP, ZFS (which is a memory hog) and Squid. This box is also used as file server over NFS. - also am running FreeBSD Jail'ed environment which is effectively similar to running Solaris Containers which also takes memory.
Yeah Solaris would have been cool but OpenSolaris is DEAD! - Thanks Oracle. Nexenta Core 3 would have been the way to go but I knew that Munin, Zabbix, and Cacti were readily and easily available in FreeBSD so I went down that route first. Only later I decided to run Xwiki in Tomcat when I started getting the errors.
If I manage to get employed this year I plan on building a dedicated box for my wiki anyway using Solaris 11 or Nexenta Core 3 (undecided currently) with GlassFishV3 and install about 8GB memory. That should be fine then as I can run multiple 'Solaris Zones' then have one DB instance for postgresql and 2 for my seperate instances of tomcat.
Thanks UnSpawn for that but Monit unfortunately takes memory!
Heh, not using any memory wasn't a posted requirement ;-p
Quote:
Originally Posted by kayasaman
I had to disable (..) basically any other non-essential service to make room for Tomcat (..) Bind, NTP, ZFS (..) Squid. (..) file server over NFS (..) FreeBSD Jail'ed environment (..) nearly 2GB of swap used
Once processes start swapping constantly things spin out of control. Java eats RAM like it's 1999, Squid needs RAM for caching (expensive I/O), too many services on that machine IMHO. Unless you're infatuated with Java or require specific Xwiki functionality you can't get otherwise why not try Wiki SW that doesn't hog memory?..
Quote:
Originally Posted by kayasaman
Solaris would have been cool but OpenSolaris is DEAD
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.