пт, 24 апр. 2020 г. в 05:25, Christopher Schultz <ch...@christopherschultz.net>:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Rafael,
>
> On 4/23/20 17:18, Rafael Oliveira wrote:
> > I does not happen every time, but it happens, actually it happened
> > twice during a couple of deployments and only in one instance of
> > several that I have.
> >
> > I could not reproduce in a safe and close environment, I got this
> > error in a server with multiple users and multiple tasks and
> > concurred environment.
> >
> > The point is raise a question is this echo $! > "CATALINA_PID" the
> > best approach to get the PID?
>
> It's literally the only way to do it.
>
> $! doesn't get you the PID of some random other process that was
> recently started. It's exactly what it says it is: the PID of the last
> process that was backgrounded by the currently-executing script.
>
> If your pid-file contains the wrong PID, it's probably because
> something overwrite overwrote it after it was initially-generated.
>
> If you try to start Tomcat twice, for example, like this:
>
> $ CATALINA_HOME/bin/startup.sh
> [...]
> $ cat $CATALINA_PID
> 1234
> $ CATALINA_HOME/bin/startup.sh
> [...]
> $ cat $CATALINA_PID
> 2345
> $ ps -ef | grep "catalina.base=$CATALINA_HOME"
> 1234
>
> The startup script will always overwrite the PID file because it can't
> tell if it's going to fail (which it will, since the shutdown port is
> already bound to the first-launched instance).
>
> In the case above, the PID-file is clobbered by the second process
> while the first process continues to run (and be the correct process
> id). There isn't much to be done about that without resorting to
> drastic measures, such as having Tomcat write its own PID file after
> it starts up (enough), and getting the PID from Java isn't
> super-straightforward until Java 9. :(

1. I do not know what version of Tomcat the OP is using, but the
current catalina.sh has some protection from double runs:

          ps -p $PID >/dev/null 2>&1
          if [ $? -eq 0 ] ; then
            echo "Tomcat appears to still be running with PID $PID.
Start aborted."
            echo "If the following process is not a Tomcat process,
remove the PID file and try again:"
            ps -f -p $PID
            exit 1
          else

There is some time window between this check and the launch of Java
process (when the PID file is being written out), so there will always
be a race condition here.

2. If one does not like how Tomcat forks a separate shell process for
a java executable, it is possible to start it with "catalina.sh run".
In this case java is started in the same process (and no PID file is
being written) and all control over this process should be done in
your own code  YMMV.

3. Tomcat can be configured as a service (daemon) and started with
Apache Commons Daemon executable (jsvc).  jsvc knows how to manage a
PID file. See daemon.sh for an example.

> and getting the PID from Java isn't super-straightforward until Java 9. :(

4. With our own cat's food: Tomcat-Native has the method:

org.apache.tomcat.jni.Stdlib.getpid()

:)

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to