You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2010/09/28 18:59:55 UTC
svn commit: r1002263 - /tomcat/trunk/bin/catalina.sh
Author: markt
Date: Tue Sep 28 16:59:54 2010
New Revision: 1002263
URL: http://svn.apache.org/viewvc?rev=1002263&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49728
Improve PID file handling, particularly when another process creates it and Tomcat is unable to write to the file.
Modified:
tomcat/trunk/bin/catalina.sh
Modified: tomcat/trunk/bin/catalina.sh
URL: http://svn.apache.org/viewvc/tomcat/trunk/bin/catalina.sh?rev=1002263&r1=1002262&r2=1002263&view=diff
==============================================================================
--- tomcat/trunk/bin/catalina.sh (original)
+++ tomcat/trunk/bin/catalina.sh Tue Sep 28 16:59:54 2010
@@ -316,8 +316,29 @@ elif [ "$1" = "start" ] ; then
if [ ! -z "$CATALINA_PID" ]; then
if [ -f "$CATALINA_PID" ]; then
- echo "PID file ($CATALINA_PID) found. Is Tomcat still running? Start aborted."
- exit 1
+ echo "Existing PID file ($CATALINA_PID) found during start."
+ if [ -s "$CATALINA_PID" ]; then
+ PID="`cat "$CATALINA_PID"`"
+ if ps -p $PID > /dev/null; then
+ echo "Tomcat appears to still be running with PID $PID. Start aborted."
+ exit 1
+ else
+ if [ -w "$CATALINA_PID" ]; then
+ echo "Removing stale PID file."
+ rm "$CATALINA_PID"
+ else
+ echo "Unable to remove stale PID file. Start aborted."
+ exit 1
+ fi
+ fi
+ else
+ if [ -w "$CATALINA_PID" ]; then
+ echo "Removing empty PID file."
+ rm "$CATALINA_PID"
+ else
+ echo "Unable to remove empty PID file. Start will continue."
+ fi
+ fi
fi
fi
@@ -373,15 +394,19 @@ elif [ "$1" = "stop" ] ; then
fi
if [ ! -z "$CATALINA_PID" ]; then
- if [ -f "$CATALINA_PID" ]; then
- kill -0 `cat $CATALINA_PID` >/dev/null 2>&1
- if [ $? -gt 0 ]; then
- echo "PID file ($CATALINA_PID) found but no matching process was found. Stop aborted."
+ if [ -s "$CATALINA_PID" ]; then
+ if [ -f "$CATALINA_PID" ]; then
+ kill -0 `cat $CATALINA_PID` >/dev/null 2>&1
+ if [ $? -gt 0 ]; then
+ echo "PID file ($CATALINA_PID) found but no matching process was found. Stop aborted."
+ exit 1
+ fi
+ else
+ echo "\$CATALINA_PID was set ($CATALINA_PID) but the specified file does not exist. Is Tomcat running? Stop aborted."
exit 1
fi
else
- echo "\$CATALINA_PID was set ($CATALINA_PID) but the specified file does not exist. Is Tomcat running? Stop aborted."
- exit 1
+ echo "PID file ($CATALINA_PID) is empty and has been ignored."
fi
fi
@@ -397,7 +422,11 @@ elif [ "$1" = "stop" ] ; then
while [ $SLEEP -ge 0 ]; do
kill -0 `cat $CATALINA_PID` >/dev/null 2>&1
if [ $? -gt 0 ]; then
- rm $CATALINA_PID
+ if [ -w "$CATALINA_PID" ]; then
+ rm "$CATALINA_PID"
+ else
+ echo "Tomact stopped but PID file could not be removed ($CATALINA_PID)."
+ fi
break
fi
if [ $SLEEP -gt 0 ]; then
@@ -420,7 +449,11 @@ elif [ "$1" = "stop" ] ; then
if [ -f "$CATALINA_PID" ]; then
echo "Killing: `cat $CATALINA_PID`"
kill -9 `cat $CATALINA_PID`
- rm $CATALINA_PID
+ if [ -w "$CATALINA_PID" ]; then
+ rm $CATALINA_PID
+ else
+ echo "Tomact was killed but PID file could not be removed ($CATALINA_PID)."
+ fi
fi
fi
fi
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r1002263 - /tomcat/trunk/bin/catalina.sh
Posted by Mladen Turk <mt...@apache.org>.
On 09/29/2010 10:51 AM, Rainer Jung wrote:
> On 28.09.2010 19:41, Mladen Turk wrote:
>> Next the code
>> > echo "Tomact stopped but PID file could not be removed ($CATALINA_PID)."
>>
>> Will fail on shells that handle ( ...) like ` ... `
>
> Just as an info: you can use
>
> echo 'Tomcat stopped but PID file could not be removed ('$CATALINA_PID').'
>
Or since this is a file name variable:
echo "Tomcat stopped but PID file could not be removed \`$CATALINA_PID'."
The notion `file.name' is a usual way of reporting the paths.
Most of the coreutils behave like that.
>
> I'm not sure though, which shells really handle "(..)" or "[...]" inside a string as above in a special way. I would expect most bigger shell scripts to fail then exactly because this is so common in log outputs. So in my opinion the above was already
> safe. I only know about the $((...)) idiom in ksh.
Think one of the Solaris shells behaves weird in those cases.
Anyhow, since braces are usually used for subshell section, a good
practice would be to avoid them entirely.
Regards
--
^TM
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r1002263 - /tomcat/trunk/bin/catalina.sh
Posted by Rainer Jung <ra...@kippdata.de>.
On 28.09.2010 19:41, Mladen Turk wrote:
> Next the code
> > echo "Tomact stopped but PID file could not be removed ($CATALINA_PID)."
>
> Will fail on shells that handle ( ...) like ` ... `
Just as an info: you can use
echo 'Tomcat stopped but PID file could not be removed ('$CATALINA_PID').'
(one line).
Inside single quotation marks shells do not make any substitution. Since
the variable needs a substitution you can switch from the single
quotation marks to double ones or none at all on the same line (and back).
I'm not sure though, which shells really handle "(..)" or "[...]" inside
a string as above in a special way. I would expect most bigger shell
scripts to fail then exactly because this is so common in log outputs.
So in my opinion the above was already safe. I only know about the
$((...)) idiom in ksh.
Regards,
Rainer
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r1002263 - /tomcat/trunk/bin/catalina.sh
Posted by Mladen Turk <mt...@apache.org>.
On 09/28/2010 06:59 PM, markt@apache.org wrote:
> Author: markt
> Date: Tue Sep 28 16:59:54 2010
> New Revision: 1002263
>
> URL: http://svn.apache.org/viewvc?rev=1002263&view=rev
> Log:
> Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49728
> Improve PID file handling, particularly when another process creates it and Tomcat is unable to write to the file.
>
Why not creating a function that would manage a pid file?
Seems to me there are lots of redundant code here.
> + if [ -w "$CATALINA_PID" ]; then
> + echo "Removing stale PID file."
> + rm "$CATALINA_PID"
> + else
> + echo "Unable to remove stale PID file. Start aborted."
> + exit 1
> + fi
Also I would rater check the return value from rm "$CATALINA_PID"
then checking if the file is writable.
File can be writable, but locked, so the rm would fail.
Also ro files and trigger rm interactive mode
'rm: remove write-protected regular file 'file' ?'
Using rm -f "$CATALINA_PID" and checking for $? != 0 would
handle all the cases..
eg, instead the upper code use:
rm -f "$CATALINA_PID" 2>/dev/null
if [ $? != 0 ]; then
echo "Unable to remove stale PID file. Start aborted."
exit 1
fi
Next the code
> echo "Tomact stopped but PID file could not be removed ($CATALINA_PID)."
Will fail on shells that handle ( ...) like ` ... `
Regards
--
^TM
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r1002263 - /tomcat/trunk/bin/catalina.sh
Posted by Mladen Turk <mt...@apache.org>.
On 09/28/2010 06:59 PM, markt@apache.org wrote:
> Author: markt
> + PID="`cat "$CATALINA_PID"`"
> + if ps -p $PID> /dev/null; then
> + echo "Tomcat appears to still be running with PID $PID. Start aborted."
> + exit 1
if $CATALINA_PID points to the file that has invalid content
the following will be shown:
ERROR: Process ID list syntax error.
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
The code should actually be
ps -p $PID > /dev/null 2>&1
if [ $? -ne 0 ]; then ...
This is to be consistent with the remaining of the tests
and to not get junk from the ps output in case of failure.
Regards
--
^TM
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org