-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Konstantin,

On 4/24/20 11:27, Konstantin Kolinko wrote:
> пт, 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.

Yes, but it fails-safe. That is, Tomcat will not start if it thinks
another Tomcat is already running. If there is a race, a second
attempt to start Tomcat will likely succeed.

> 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()

Interesting, although that does require that tcnative is available.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl6jF6IACgkQHPApP6U8
pFgzhRAApf5Rzn/0n8S2gCKorMlHftPWSo9BUoYkHBAqlMpYzIzVIxvpxujPBC5n
P1/ifE+VqK98G6nkEKuJwQI3IZXNvd8zW3x6/rcVKhHG/0d81rYw2AeAxbAt4cUS
JqQqwpbInOl0dpcWnkQic2KnjJP+qI25DGL0U3Rsx3J9T8tfQ11Dv9AIlepAO4uu
KXgcs2JGlwv6vOzV2MkFuw6/0LkrDV4jlV1uYJFHj9CopO9x1ofNGupu6WjtlqiM
FbIuPudmus8tmPP/TZ3lSjuj2Aw2kVq+7vBgsKLAVo/KIXifTP7DXXdiI4J2hcdG
su+806PDsuEDwOKyMuWXhKzn2TuE+UVyxYMMTu1VrbX3GlIxPszjswpQgFAvLbNj
GEQNarV18E/Ni6Zvw4PM1jJiMtcKyoPxUcVWbBkwDrMcqymPLdWrZEp/lBrwMsw6
XQHLzqHNl8ZUW6YIwd2a0VgycqJzPKVP8zfhvgUzf2NxtMzCNwckebZ4lvhD5S+T
cZqCwfR+J8VNOlhB+52JLRl1aotqX5LYMI8HNwQAanfbg5e3940FeBeIZF9XZSb4
+IsVNlBdpS1bsv6yAGGrSk2drM85cHW6pBiEGhqfSfDDlm8xSURNaQLM27GWEQgD
T2gbijw711xG3U3bRjoZa445IQ7ZN8phs9mA2n6eDi1WGXwM0Bs=
=cdsU
-----END PGP SIGNATURE-----

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

Reply via email to