Link to home
Start Free TrialLog in
Avatar of saharey
saharey

asked on

start server without PID and server.pid file

Before starting my tomcat server I want to see that "server.pid" file is not present and also that the no process is running with PID mentioned in this server.pid, I am doing like this which is not working for me, when I execute command
./serv.sh start
(Below is code snippet)
//----------------------------------------------------------------
startServer()
{
        # Verify that the sever isn't already started
        if [ ! -z "$CATALINA_PID" ]; then
          if [ -f "$CATALINA_PID" ]; then
            echo "PID file ($CATALINA_PID) found. Trying to delete $CATALINA_PID file"
            rm -f "${SERVER_PID}"
          fi
        fi
        RET=`isRunning`

        if [ "${RET}" -eq 1 ]
        then
                fail "The server is already running."
        fi

        echo "Using CATALINA_OPTS:"
        for OPT in $CATALINA_OPTS
        do
                echo "     $OPT"
        done

        echo "Starting server..."

        "$CATALINA" start

        rc=$?
        echo "Return code is $rc"
}

//---------------------------------------------------------------------

Even though the server is already running it starts the server again, also attaching my main shell script.
Can anyone help me out in this respect.
serv.sh
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,
seems that you're removing the server's PID file before checking whether it's running or not. (Why do you use two different names for the same file, by the way?)
"isRunning" will never find a PID file this way, so it can't really do waht it's supposed to do.
Change the sequence - call "isRunning" before trying to remove the PID file!
wmp
Avatar of saharey
saharey

ASKER

Where should I put this piece of code in function startServer():

 if [ ! -z "$CATALINA_PID" ]; then
          if [ -f "$CATALINA_PID" ]; then
            echo "PID file ($CATALINA_PID) found."
            rm -f "${CATALINA_PID}"
          fi
        fi

because wherever I use the above code snippet result is either same or somewhere else the things are failing. Or maybe my approach towards my problem is wrong do I need to make changes in above code if yes what can be the changes?
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of saharey

ASKER

Thanks for quick reply wmp, what if I use the code in this fashion

startServer()
{
        # Verify that the sever isn't already started
        #RET=`$PS | grep java | grep "$AO_HOME" | grep -v grep`
        RET=`isRunning`

        if [ "${RET}" -eq 1 ]
        then
                fail "The server is already running."
        else
           if [ ! -z "$CATALINA_PID" ]; then
             if [ -f "$CATALINA_PID" ]; then
               echo "PID file ($CATALINA_PID) found. Trying to delete $CATALINA_PID file"
               rm -f "${CATALINA_PID}"
             fi
           fi

        fi

        echo "Using CATALINA_OPTS:"
        for OPT in $CATALINA_OPTS
        do
                echo "     $OPT"
        done

        echo "Starting server..."

        "$CATALINA" start

        rc=$?
        echo "Return code is $rc"
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of saharey

ASKER

This solution works perfectly alright if say for unforeseen circumstance PID is being killed and “server.pid” file is present then it works fine, but if I take reverse scenario say PID is running and someone accidently deleted “server.pid” and when I execute
Serv.sh start
then the second instance of tomcat is started with new PID, so any changes in my code to check this scenario.
Yes,
that's because you search the processlist only for the PID.
Maybe like this in "isRunning" (only a suggestion) -
isRunning ()
{
   if [ ! -z "$CATALINA_PID" ]; then
   SEARCH=`cat "${SERVER_PID}" 2>/dev/null`
   else
   SEARCH=$CATALINA
  fi
   RESULT=`${PS} | grep ${SEARCH}`
   if [ -n "${RESULT}" ]; then
      # if we are here, then server is running
      echo 1
   else
      # if we are here, then server is not running
      echo 0
   fi
}
 
You should test it carefully. I just don't know if "catalina.sh" (and its path as in $CATALINA) remain in the processlist or not. Maybe you'll need some other criterion to search for.
Anyway, the idea is - if the PID file does not exist anymore, search for the process' name instaed.
 
Avatar of saharey

ASKER


1. If my tomcat server is already running and still I execute "serv.sh start" command it tries to start tomcat again with following output on console:
//-------------------------------------------------------
[root@rhel4 bin]# ./serv.sh start
./server.sh: line 177: [: 0
1: integer expression expected
PID file (/home/xlri/XLRISoftware/rat/rogan/bin/server.pid) found. Trying to delete /home/xlri/XLRISoftware/rat/rogan/bin/server.pid file
Using CATALINA_OPTS:
     -DJXTA_HOME=/home/xlri/XLRISoftware/rat/rogan/server/.jxta
     -Dmessages.home.dir=/home/xlri/XLRISoftware/rat/rogan
     -Djava.library.path=:/home/xlri/XLRISoftware/rat/rogan/lib/x86/linux
Starting server...
Using CATALINA_BASE:   /home/xlri/XLRISoftware/rat/rogan/tomcat
Using CATALINA_HOME:   /home/xlri/XLRISoftware/rat/rogan/tomcat
Using CATALINA_TMPDIR: /home/xlri/XLRISoftware/rat/rogan/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.6.0_16
Using CLASSPATH:       /home/xlri/XLRISoftware/rat/rogan/tomcat/bin/bootstrap.jar
Return code is 0
//-------------------------------------------------------
So now there are 2 instance of same tomcat is running now, but in actual if I try to start my server when it is already running it should through and error like this:

Error - The server is already running.

2. As you suggested to look for exact syntax in process list , this is what my process list looks like :

root     30006     1  0 Aug29 ?        00:14:28 /usr/java/jdk1.6.0_16/bin/java -Djava.util.logging.config.file=/home/xlri/XLRISoftware/rat/rogan/tomcat/conf/logging.properties -server -Xms1024m -Xmx1280m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/xlri/XLRISoftware/rat/rogan/tomcat/logs -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -DJXTA_HOME=/home/xlri/XLRISoftware/rat/rogan/server/.jxta -Dmessages.home.dir=/home/xlri/XLRISoftware/rat/rogan -Djava.library.path=:/home/xlri/XLRISoftware/rat/rogan/lib/x86/linux -Djava.endorsed.dirs=/home/xlri/XLRISoftware/rat/rogan/tomcat/endorsed -classpath /home/xlri/XLRISoftware/rat/rogan/tomcat/bin/bootstrap.jar -Dcatalina.base=/home/xlri/XLRISoftware/rat/rogan/tomcat -Dcatalina.home=/home/xlri/XLRISoftware/rat/rogan/tomcat -Djava.io.tmpdir=/home/xlri/XLRISoftware/rat/rogan/tomcat/temp org.apache.catalina.startup.Bootstrap start

I am unable to decide which syntax to grep in this case.
My "isRunning" and "startServer" functions looks like this:
I know it was just a suggestion from you if any help in this regard will be highly appreciated.


############################################################
# isRunning - Server condition
############################################################
isRunning ()
{
   #PID=`cat "$CATALINA_PID" 2>/dev/null`
   if [ ! -z "$CATALINA_PID" ]; then
      SEARCH=`cat "${SERVER_PID}" 2>/dev/null`
    else
      SEARCH=$CATALINA
   fi
   RESULT=`${PS} | grep $SEARCH`
   if [ -n "${RESULT}" ]; then
      # if we are here, then server is running
      echo 1
   else
      # if we are here, then server is not running
      echo 0
   fi
}
#set -x
############################################################
# startServer - Starts the server in the background
############################################################
startServer()
{
        # Verify that the sever isn't already started

        if [ ! -z "$CATALINA_PID" ]; then
          if [ -f "$CATALINA_PID" ]; then
            echo "PID file ($CATALINA_PID) found. Trying to delete $CATALINA_PID file"
            rm -f "${SERVER_PID}"
          fi
        fi
        RET=`isRunning`

        if [ "${RET}" -eq 1 ]
        then
               fail "The server is already running."
	 else
           if [ ! -z "$CATALINA_PID" ]; then
             if [ -f "$CATALINA_PID" ]; then
               echo "PID file ($CATALINA_PID) found. Trying to delete $CATALINA_PID file"
               rm -f "$CATALINA_PID"
             fi
           fi

        fi

        echo "Using CATALINA_OPTS:"
        for OPT in $CATALINA_OPTS
        do
                echo "     $OPT"
        done

        echo "Starting server..."

        "$CATALINA" start

        rc=$?
        echo "Return code is $rc"
}

Open in new window

You STILL remove $CATALINA_PID in the wrong place. How should "isRunning" find a PID file when you delete it before calling "isRunning"??
Move " RET=`isRunning` " up to line 28 or so, as I suggested in http:#a33557118

As for the process list - You could grep for something like "org.apache.catalina.startup" or so.
 
Avatar of saharey

ASKER

Sorry it’s my mistake while composing comment for this question I forget to swap the sequence, but in actual my “startServer” function looks like this:
startServer()
{
        # Verify that the sever isn't already started
	RET=`isRunning`

       if [ "${RET}" -eq 1 ]
        then
               fail "The server is already running."
	 else
           if [ ! -z "$CATALINA_PID" ]; then
             if [ -f "$CATALINA_PID" ]; then
               echo "PID file ($CATALINA_PID) found. Trying to delete $CATALINA_PID file"
               rm -f "$CATALINA_PID"
             fi
           fi

        fi

        echo "Using CATALINA_OPTS:"
        for OPT in $CATALINA_OPTS
        do
                echo "     $OPT"
        done

        echo "Starting server..."

        "$CATALINA" start

        rc=$?
        echo "Return code is $rc"
}

Open in new window

OK,
so try in "isRunning"
isRunning ()
{
   #PID=`cat "$CATALINA_PID" 2>/dev/null`
   if [ ! -z "$CATALINA_PID" ]; then
      SEARCH=`cat "${SERVER_PID}" 2>/dev/null`
    else
      SEARCH="org.apache.catalina.startup"
  fi
   RESULT=`${PS} | grep $SEARCH`
   if [ -n "${RESULT}" ]; then
      # if we are here, then server is running
      echo 1
   else
      # if we are here, then server is not running
      echo 0
   fi
}
You could also put "org.apache.catalina.startup" in a variable during your script's initialization steps, to facilitate possible changes in the future.
 
 
Avatar of saharey

ASKER

I cannot search for

"org.apache.catalina.startup"

as there are chances that two or three instance of my application will be running on same box in different folders, so may be XLRI_HOME value can be searched.
Avatar of saharey

ASKER

Even after making changes in "isRunning" function as suggested still server is starting even though already running.
Any other thoughts?
Look there: http://wrapper.tanukisoftware.com/doc/english/integrate-start-stop-nix.html
Should be a lot easier than reinventing the wheel.
Avatar of saharey

ASKER

After going through the doc of wrapper, I cannot use this wrapper class with my application also need to support four different flavors of OS Win, Linux, Sol and Mac.
It is enough to use wrapper to start/stop tomcat. There is no need to instrument wrapper inside your application.
By the way it supports all systems mentioned, and even exotics like OS/400 and Tru64
Avatar of saharey

ASKER

Actually I need to take lots of pain from upper management to get it approved and to use these wrapper classes with our application.
I know how stubborn the senior management is.
Avatar of saharey

ASKER

Also this is not freeware it requires license to use it in our commercial application, which makes it is more tuff to get approval.
your application is a .war file(S)
your customer runs it inside tomcat which in turn runs well inside tanuki product.
Avatar of saharey

ASKER

Another solution if I can get apart from using wrapper class will be great help.
Avatar of saharey

ASKER

Finally again I end up without any solution to my problem.
What are you trying to accomplish?
Restart catalina (aka tomcat) if it has crashed or to juggle with its PID file?
Avatar of saharey

ASKER


Both,
First Scenario:
if tomcat has crashed and there is no process list but “server.pid” file is present which is now achievable by my code
startServer()
{
        # Verify that the sever isn't already started
 RET=`isRunning`
       if [ "${RET}" -eq 1 ]
        then
               fail "The server is already running."
  else
           if [ ! -z "$CATALINA_PID" ]; then
             if [ -f "$CATALINA_PID" ]; then
               echo "PID file ($CATALINA_PID) found. Trying to delete $CATALINA_PID file"
               rm -f "$CATALINA_PID"
             fi
           fi
        fi
        echo "Using CATALINA_OPTS:"
        for OPT in $CATALINA_OPTS
        do
                echo "     $OPT"
        done
        echo "Starting server..."
        "$CATALINA" start
        rc=$?
        echo "Return code is $rc"
}
Second Scenario:
Say by accidently “server.pid” is deleted but tomcat is still running with process list,  and I execute command
./serv.sh start
then above startServer() function does not behave as expected, in this case it should show the error message “server running but server.pid is not present” instead it starts server again.

Also above code is not working properly on Solaris, if PID is being killed and when I try to start server it should start the server by removing “server.pid”, but it shows me error message “server already running”

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of saharey

ASKER

I did not get do I need to put kill -0 'cat catalina.pid' in isRunning function
what your function is doing? what it does?
Avatar of saharey

ASKER

Which function you are talking of.
Avatar of saharey

ASKER

isRunning functions checks weather the server is running by looking at PID from server.pid file
looking? you serious?
Avatar of saharey

ASKER

Any more thoughts?
i suppose your isRunning function is WRONG
please post it to get a fixed version.
Avatar of saharey

ASKER

here is my isRunning function
isRunning ()
{
   PID=`cat "${CATALINA_PID}" 2>/dev/null`
   if [ -z "$PID" ]; then
      echo 0
      return
   fi
   RESULT=`$PS | grep -w $PID`
   if [ -n "$RESULT" ]; then
      # if we are here, then server is running
      echo 1
   else
      # if we are here, then server is not running
      echo 0
   fi
}

Open in new window

good ways to check if catalina runs

- connect to the port it is listening on using netcat, telnet, tcpcat or whatever utility your usially use
- grep netstat or sockstat output for the listen line
Avatar of saharey

ASKER

Catalina ports during installation of application get changed  during every new installation,  so I will have to introduced a new variable in my script to catch the user defined port during installation, which will became a bit tedious with respect to code on installation part.
Avatar of saharey

ASKER

This is not good, I hardly get my issues resolved on this paid forum.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of saharey

ASKER

Thanks for advice, I will create a new question and will distribute the points to all experts who tried to answer my question.
Avatar of saharey

ASKER

Experts unable to provide me with accurate solution , which is problem on this site experts usually run away after few tries.