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
./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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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.
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/r
Using CATALINA_OPTS:
-DJXTA_HOME=/home/xlri/XLR
-Dmessages.home.dir=/home/
-Djava.library.path=:/home
Starting server...
Using CATALINA_BASE: /home/xlri/XLRISoftware/ra
Using CATALINA_HOME: /home/xlri/XLRISoftware/ra
Using CATALINA_TMPDIR: /home/xlri/XLRISoftware/ra
Using JRE_HOME: /usr/java/jdk1.6.0_16
Using CLASSPATH: /home/xlri/XLRISoftware/ra
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/
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"
}
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.start up" or so.
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.start
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"
}
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.catalin a.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.start up" in a variable during your script's initialization steps, to facilitate possible changes in the future.
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.catalin
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.start
ASKER
I cannot search for
"org.apache.catalina.start up"
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.
"org.apache.catalina.start
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.
ASKER
Even after making changes in "isRunning" function as suggested still server is starting even though already running.
Any other thoughts?
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.
Should be a lot easier than reinventing the wheel.
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
By the way it supports all systems mentioned, and even exotics like OS/400 and Tru64
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.
I know how stubborn the senior management is.
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.
your customer runs it inside tomcat which in turn runs well inside tanuki product.
ASKER
Another solution if I can get apart from using wrapper class will be great help.
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?
Restart catalina (aka tomcat) if it has crashed or to juggle with its PID file?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ASKER
Which function you are talking of.
ASKER
isRunning functions checks weather the server is running by looking at PID from server.pid file
looking? you serious?
ASKER
Any more thoughts?
i suppose your isRunning function is WRONG
please post it to get a fixed version.
please post it to get a fixed version.
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
}
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
- 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
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.
ASKER
This is not good, I hardly get my issues resolved on this paid forum.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for advice, I will create a new question and will distribute the points to all experts who tried to answer my question.
ASKER
Experts unable to provide me with accurate solution , which is problem on this site experts usually run away after few tries.
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