I have a handful of shell scripts that start WebLogic managed servers. I need to design a master script that does the following:

  • Executes the shell script for a component\managed server
  • Checks to see if the port for that component is listening for 2 minutes (this can be any value but none of these services should take longer than 2 mins to start)
  • If the service starts within the 2 minutes, continue on and start the next service otherwise write a log message that the service failed to start and then continue on

The script will look something like this:

svcs = some_array_of_svcs

log  = '/path_to_log/log.txt'

' start each service'
for each $svc in $svcs
   echo "starting $svc" >> ${log}
   . /path_to_scripts/$svc
' check to see if the service started
loop for max 2 mins
   if port is listening
      echo 'Service started successfully' >> ${log}
      start the next service
      echo 'Service did not start within the specified timeout' >> ${log}
      start the next service
   end if

I need the code to check the port status for n minutes per service.


netcat to the rescue... Netcat is very similar to telnet, but with a few extra crazy options. One of particular use in this case is -z, which just checks to see if the connection works. Along with the timeout variable, you can have the system repeatedly check if the service is listening.

I have ssh enabled locally, but not telnet.

$ nc -zw10 localhost 22
$ echo $?
$ nc -zw10 localhost 23
$ echo $?

And for the a bit more clarity on the testing... This is assuming the 2 minute timeout you mentioned earlier. It checks 4 times with half a minute timeout each time. Datestamps would likely be better, but this is a start.

for i in {1..4}; do
  nc -zw30 localhost 22
  [[ $x -eq 0 ]] && break
if [[ $x -eq 0 ]]; then
  echo 'Service started successfully' >> ${log}
  echo 'Service did not start within the specified timeout' >> ${log}
start next service
  • Thanks, this is a great start. Just for clarity sake, the 2 minutes is simply the max timeout we want to spend checking to see if the port is up and listening. If the port is up in 10 secs, we want to break out of the loop and start the next service. Does your code do that? – user71978 Jun 9 '14 at 22:57
  • That's what the "break" is for in the for loop. As I mentioned earlier, the datestamp (or timestamp) in a while loop would be better for the actual timeout, since the above will finish VERY quickly if the port is refused each time. The timeout is just there if the port is open, but you aren't getting a response yet from the daemon. The exit code is stored in x, so you can check what the last exit condition was later in the if section. – Falsenames Jun 10 '14 at 0:45

Another way to check a port's status is with the netstat utility.

user@debian:~$ netstat -4 -l --tcp -p # Show programs listening to IPv4/TCP ports
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address    State       
tcp        0      0 *:53             *:*            LISTEN   2389/named      
tcp        0      0 *:22             *:*            LISTEN   2936/sshd       
tcp        0      0 *:5432           *:*            LISTEN   2475/postgres   
tcp        0      0 *:25             *:*            LISTEN   2961/exim4 

Here I see that DNS, SSH, PosgtreSQL and Mail daemons are up since they are listening to their respective ports. Moreover the last column tells me which application is listening to each specific port.

The script for checking a single service would then look like:

PORT=... the port we need to check ...
# Current time in seconds + 15 minutes
TRYUNTIL=$(( $(date +%s) - (60*15) ))
# 0 is down, 1 is up

# While the service is still down and the current time is before our limit
while [[ (( $STATUS = 0 )) && (( $(date +%s) < $TRYUNTIL ))  ]]
   STATUS=$(netstat -4 -l --tcp | grep "*:$PORT" | wc -l)
   # Lets not overload the system with constant checking,
   # if the service is still down
   if [[ $STATUS = 0 ]]; then
        sleep 5s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.