Solved

start server without PID and server.pid file

Posted on 2010-08-30
41
655 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:saharey
  • 22
  • 8
  • 6
  • +1
41 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33556821
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
0
 

Author Comment

by:saharey
ID: 33557086
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?
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 267 total points
ID: 33557118
Exchange the sequence in startServer
startServer()
{
        # Verify that the sever isn't already started
        RET=`isRunning`

        if [ "${RET}" -eq 1 ]
        then
                fail "The server is already running."
        fi
       # Try to remove the PID file
        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
        echo "Using CATALINA_OPTS:"
        for OPT in $CATALINA_OPTS
        do
                echo "     $OPT"
        done

        echo "Starting server..."

        "$CATALINA" start

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

0
 

Author Comment

by:saharey
ID: 33557236
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"
}
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 267 total points
ID: 33557252
OK, no problem!
Quite more elegant than my version!
0
 

Author Comment

by:saharey
ID: 33557285
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33557345
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.
 
0
 

Author Comment

by:saharey
ID: 33575219

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

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33575372
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.
 
0
 

Author Comment

by:saharey
ID: 33575488
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

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33575551
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.
 
 
0
 

Author Comment

by:saharey
ID: 33576039
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.
0
 

Author Comment

by:saharey
ID: 33576357
Even after making changes in "isRunning" function as suggested still server is starting even though already running.
Any other thoughts?
0
 
LVL 61

Expert Comment

by:gheist
ID: 33610547
Look there: http://wrapper.tanukisoftware.com/doc/english/integrate-start-stop-nix.html
Should be a lot easier than reinventing the wheel.
0
 

Author Comment

by:saharey
ID: 33615500
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.
0
 
LVL 61

Expert Comment

by:gheist
ID: 33615585
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
0
 

Author Comment

by:saharey
ID: 33615719
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.
0
 

Author Comment

by:saharey
ID: 33615732
Also this is not freeware it requires license to use it in our commercial application, which makes it is more tuff to get approval.
0
 
LVL 61

Expert Comment

by:gheist
ID: 33615790
your application is a .war file(S)
your customer runs it inside tomcat which in turn runs well inside tanuki product.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:saharey
ID: 33660379
Another solution if I can get apart from using wrapper class will be great help.
0
 

Author Comment

by:saharey
ID: 33669643
Finally again I end up without any solution to my problem.
0
 
LVL 61

Expert Comment

by:gheist
ID: 33669787
What are you trying to accomplish?
Restart catalina (aka tomcat) if it has crashed or to juggle with its PID file?
0
 

Author Comment

by:saharey
ID: 33670079

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”

0
 
LVL 61

Assisted Solution

by:gheist
gheist earned 117 total points
ID: 33670750
isRunning should be
 kill -0 `cat catalina.pid`

you should not DELETE pid file catalina can do it if it does not fail to open listen ports.
0
 

Author Comment

by:saharey
ID: 33670792
I did not get do I need to put kill -0 'cat catalina.pid' in isRunning function
0
 
LVL 61

Expert Comment

by:gheist
ID: 33671102
what your function is doing? what it does?
0
 

Author Comment

by:saharey
ID: 33671322
Which function you are talking of.
0
 

Author Comment

by:saharey
ID: 33671600
isRunning functions checks weather the server is running by looking at PID from server.pid file
0
 
LVL 61

Expert Comment

by:gheist
ID: 33671792
looking? you serious?
0
 

Author Comment

by:saharey
ID: 33724471
Any more thoughts?
0
 
LVL 61

Expert Comment

by:gheist
ID: 33724699
i suppose your isRunning function is WRONG
please post it to get a fixed version.
0
 

Author Comment

by:saharey
ID: 33883891
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

0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 33903482
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
0
 

Author Comment

by:saharey
ID: 33911412
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.
0
 

Author Comment

by:saharey
ID: 33935745
This is not good, I hardly get my issues resolved on this paid forum.
0
 
LVL 26

Assisted Solution

by:skullnobrains
skullnobrains earned 116 total points
ID: 33990258
<quote>
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.
<quote>

- you can specify the port to use during install
- the tomcat listen port does not change so you can just use that one
- you can use sockstat or similar utilities to detect the port on the fly
- you never mentioned you were dealing with multiple install

- the answers you already have are reasonnably good, but you need some basic skill to put them in practice
- you probably should remember that even if you paid for using this forum, the guys who answer do it for free
- you'd get better help by asking for it, giving proper information including your own skill level.

modos : i hope i'm not out of line. if that is the case, please accept apologies
0
 

Author Comment

by:saharey
ID: 33991414
Thanks for advice, I will create a new question and will distribute the points to all experts who tried to answer my question.
0
 

Author Closing Comment

by:saharey
ID: 33999005
Experts unable to provide me with accurate solution , which is problem on this site experts usually run away after few tries.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now