You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Sunil Shevante <su...@yahoo.com> on 2013/01/26 11:19:53 UTC

Restarting Tomcat from within itself?

Hi,

Is it possible to restart tomcat from within itself?

I host a JSP website and somehow the memory utilization keeps on increasing from time to time. I then have to manually restart Tomcat.

While I understand that in the long term, I should focus on fixing the memory leaks, is there a way by which I can restart Tomcat after the free memory goes below a threshold percentage?

I am using tomcat 6.0.14 on unix.

Thanks,
Sunil.

Re: Restarting Tomcat from within itself?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hassan,

On 1/28/13 10:43 AM, Hassan Schroeder wrote:
> On Mon, Jan 28, 2013 at 7:32 AM, Christopher Schultz 
> <ch...@christopherschultz.net> wrote:
> 
>>> Alternatively, you could use a preexisting tool to monitor
>>> your Tomcat instance like Nagios or Hyperic.
>> 
>> These solutions really only monitor. Yes, you can configure them
>> to "react" to certain situations by abusing the notification
>> system, but they are really meant for monitoring and
>> notification, not for automated tasks.
> 
> Unlike, say, http://mmonit.com/monit/   :-)

Hey, that's cool. Thanks for the reference.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlEGpKEACgkQ9CaO5/Lv0PCsWgCcDl0zhBl76bANry7kINfPLI3u
ObYAoLg3BGf3MDEkFzpHq2oLRqRxDUkG
=XvkL
-----END PGP SIGNATURE-----

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


Re: Restarting Tomcat from within itself?

Posted by Hassan Schroeder <ha...@gmail.com>.
On Mon, Jan 28, 2013 at 7:32 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:

>> Alternatively, you could use a preexisting tool to monitor your
>> Tomcat instance like Nagios or Hyperic.
>
> These solutions really only monitor. Yes, you can configure them to
> "react" to certain situations by abusing the notification system, but
> they are really meant for monitoring and notification, not for
> automated tasks.

Unlike, say, http://mmonit.com/monit/   :-)

-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

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


Re: Restarting Tomcat from within itself?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Andrew,

On 1/28/13 2:18 PM, Andrew Kujtan wrote:
>> -----Original Message----- From: Christopher Schultz
>> [mailto:chris@christopherschultz.net] Sent: Monday, January 28,
>> 2013 2:01 PM To: Tomcat Users List Subject: Re: Restarting Tomcat
>> from within itself?
>> 
> Andrew,
> 
> On 1/28/13 1:49 PM, Andrew Kujtan wrote:
>>>>> -----Original Message----- From: Christopher Schultz 
>>>>> [mailto:chris@christopherschultz.net] Sent: Monday, January
>>>>> 28, 2013 10:33 AM To: Tomcat Users List Subject: Re:
>>>>> Restarting Tomcat from within itself?
>>>>> 
>>>> Daniel,
>>>> 
>>>> On 1/28/13 8:19 AM, Daniel Mikusa wrote:
>>>>>>> On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:
>>>>>>> 
>>>>>>>> Hi,
>>>>>>>> 
>>>>>>>> Is it possible to restart tomcat from within itself?
>>>>>>>> 
>>>> 
>>>>> FWIW, It is possible to restart tomcat by calling,
>>>> 
>>>>> org.apache.catalina.startup.Bootstrap.main(new String[] {
>>>>> "stop" });
>>>> 
>>>>> then use a shutdown hook to start it up again,
>>>> 
>>>>> But I really don't recommend it.
> 
> What would your shutdown hook do, then?
> 
> -chris
> 
>> Just use a ProcessBuilder to call the startup script in a new
>> process.

This seems like a risky proposition (I suppose no less risky than
using -XX:OnOutOfMemoryError). If you run Tomcat under jsvc (or as a
Windows Service), I believe you can configure both of those strategies
to re-start Tomcat if it stops for some reason. In that case, a simple
System.exit() will shut-down Tomcat (or you can send a SHUTDOWN
command to your own shutdown port) and the service-monitor can
re-start it.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlEG7REACgkQ9CaO5/Lv0PDDVgCfQHgkF6c1ohuWY20iFJuVkigs
RewAnRLNVNwJye7t8YhlZoKx/C920bj7
=I1SP
-----END PGP SIGNATURE-----

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


RE: Restarting Tomcat from within itself?

Posted by Sunil Shevante <su...@yahoo.com>.
To add information to the query - I dont have access to the server. So I cannot setup jobs or change tomcat startup parameters.

I have hosted my site with a provider and I only have cpanel access. Hence I am looking for some code which would restart/reintialize tomcat from within.

Website - www.investorschoolindia.com



--- On Tue, 1/29/13, Andrew Kujtan <ak...@evertz.com> wrote:

From: Andrew Kujtan <ak...@evertz.com>
Subject: RE: Restarting Tomcat from within itself?
To: "Tomcat Users List" <us...@tomcat.apache.org>
Date: Tuesday, January 29, 2013, 12:48 AM



> -----Original Message-----
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Sent: Monday, January 28, 2013 2:01 PM
> To: Tomcat Users List
> Subject: Re: Restarting Tomcat from within itself?
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Andrew,
> 
> On 1/28/13 1:49 PM, Andrew Kujtan wrote:
> >> -----Original Message----- From: Christopher Schultz
> >> [mailto:chris@christopherschultz.net] Sent: Monday, January 28,
> >> 2013 10:33 AM To: Tomcat Users List Subject: Re: Restarting Tomcat
> >> from within itself?
> >>
> > Daniel,
> >
> > On 1/28/13 8:19 AM, Daniel Mikusa wrote:
> >>>> On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:
> >>>>
> >>>>> Hi,
> >>>>>
> >>>>> Is it possible to restart tomcat from within itself?
> >>>>>
> >
> >> FWIW, It is possible to restart tomcat by calling,
> >
> >> org.apache.catalina.startup.Bootstrap.main(new String[] { "stop"
> >> });
> >
> >> then use a shutdown hook to start it up again,
> >
> >> But I really don't recommend it.
> 
> What would your shutdown hook do, then?
> 
> - -chris

Just use a ProcessBuilder to call the startup script in a new process.

> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org

> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

> 
> iEYEAREIAAYFAlEGyvkACgkQ9CaO5/Lv0PAYJACghkS1q1PS8twbUz2ITogaw6uW
> oXoAoLD7TAO5YNi1+N4tcmEej0aBu1qr
> =23Sy
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


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


RE: Restarting Tomcat from within itself?

Posted by Andrew Kujtan <ak...@evertz.com>.

> -----Original Message-----
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Sent: Monday, January 28, 2013 2:01 PM
> To: Tomcat Users List
> Subject: Re: Restarting Tomcat from within itself?
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Andrew,
> 
> On 1/28/13 1:49 PM, Andrew Kujtan wrote:
> >> -----Original Message----- From: Christopher Schultz
> >> [mailto:chris@christopherschultz.net] Sent: Monday, January 28,
> >> 2013 10:33 AM To: Tomcat Users List Subject: Re: Restarting Tomcat
> >> from within itself?
> >>
> > Daniel,
> >
> > On 1/28/13 8:19 AM, Daniel Mikusa wrote:
> >>>> On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:
> >>>>
> >>>>> Hi,
> >>>>>
> >>>>> Is it possible to restart tomcat from within itself?
> >>>>>
> >
> >> FWIW, It is possible to restart tomcat by calling,
> >
> >> org.apache.catalina.startup.Bootstrap.main(new String[] { "stop"
> >> });
> >
> >> then use a shutdown hook to start it up again,
> >
> >> But I really don't recommend it.
> 
> What would your shutdown hook do, then?
> 
> - -chris

Just use a ProcessBuilder to call the startup script in a new process.

> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
> 
> iEYEAREIAAYFAlEGyvkACgkQ9CaO5/Lv0PAYJACghkS1q1PS8twbUz2ITogaw6uW
> oXoAoLD7TAO5YNi1+N4tcmEej0aBu1qr
> =23Sy
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


Re: Restarting Tomcat from within itself?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Andrew,

On 1/28/13 1:49 PM, Andrew Kujtan wrote:
>> -----Original Message----- From: Christopher Schultz
>> [mailto:chris@christopherschultz.net] Sent: Monday, January 28,
>> 2013 10:33 AM To: Tomcat Users List Subject: Re: Restarting
>> Tomcat from within itself?
>> 
> Daniel,
> 
> On 1/28/13 8:19 AM, Daniel Mikusa wrote:
>>>> On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:
>>>> 
>>>>> Hi,
>>>>> 
>>>>> Is it possible to restart tomcat from within itself?
>>>>> 
> 
>> FWIW, It is possible to restart tomcat by calling,
> 
>> org.apache.catalina.startup.Bootstrap.main(new String[] { "stop"
>> });
> 
>> then use a shutdown hook to start it up again,
> 
>> But I really don't recommend it.

What would your shutdown hook do, then?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlEGyvkACgkQ9CaO5/Lv0PAYJACghkS1q1PS8twbUz2ITogaw6uW
oXoAoLD7TAO5YNi1+N4tcmEej0aBu1qr
=23Sy
-----END PGP SIGNATURE-----

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


RE: Restarting Tomcat from within itself?

Posted by Andrew Kujtan <ak...@evertz.com>.

> -----Original Message-----
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Sent: Monday, January 28, 2013 10:33 AM
> To: Tomcat Users List
> Subject: Re: Restarting Tomcat from within itself?
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Daniel,
> 
> On 1/28/13 8:19 AM, Daniel Mikusa wrote:
> > On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:
> >
> >> Hi,
> >>
> >> Is it possible to restart tomcat from within itself?
> >>

FWIW, It is possible to restart tomcat by calling,

org.apache.catalina.startup.Bootstrap.main(new String[] { "stop" });

then use a shutdown hook to start it up again,

But I really don't recommend it. 

> >> I host a JSP website and somehow the memory utilization keeps on
> >> increasing from time to time. I then have to manually restart
> Tomcat.
> >>
> >> While I understand that in the long term, I should focus on fixing
> >> the memory leaks, is there a way by which I can restart Tomcat after
> >> the free memory goes below a threshold percentage?
> >
> > I wouldn't try to do this inside Tomcat.  With the tools provided by
> > the JVM (jmap or JMX), you can monitor just as easily from outside
> > Tomcat.  Instead, create a monitoring script to watch the Tomcat
> > process.  When memory usage hits a certain level, dump the heap (so
> > you can analyze your memory problem later) and restart Tomcat.
> 
> +1
> 
> If you want to chance it, you could set -XX:OnOutOfMemoryError=<cmd>
> and write a script that tries to dump your heap (or just have the JVM
> do that for you) and then bounce Tomcat.
> 
> Note that not all OOMEs are life-threatening for the JVM.
> 
> I have experienced two kinds of OOMEs:
> 
> 1) Truly out of memory: your app simply needs more heap. In these
>    cases, the JVM generally goes into GC panic for a short amount of
>    time trying to free-up memory and then finally throws OOME.
>    Nothing meaningful seems to work after that: DBCPs run dry,
>    etc. In these cases, I have had to restart my JVM completely
>    in order for any users to get anything meaningful done.
> 
> 2. Transient memory exhaustion: this happens when we have some careless
>    code that loads 100,000 records from a single database query all
>    into a big java.util.List or something like that. In these cases,
>    a single thread gets an OOME, the List gets GC'd very quickly, and
>    the server (and JVM) are fine. We generally do restart our JVMs when
>    this happens, but we can wait until off-hours when fewer users are
>    likely to be impacted in any way.
> 
> > Alternatively, you could use a preexisting tool to monitor your
> Tomcat
> > instance like Nagios or Hyperic.
> 
> These solutions really only monitor. Yes, you can configure them to
> "react" to certain situations by abusing the notification system, but
> they are really meant for monitoring and notification, not for
> automated tasks.
> 
> >> I am using tomcat 6.0.14 on unix.
> >
> > This is really old, upgrade.
> 
> +10
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
> 
> iEYEAREIAAYFAlEGmisACgkQ9CaO5/Lv0PAMeACfflakV2t8RT5kjF/wQf8joYl9
> swgAmgL1pwbvX5yLe2jUSGaOXXr5SDzf
> =MaHM
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


Re: Restarting Tomcat from within itself?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Daniel,

On 1/28/13 8:19 AM, Daniel Mikusa wrote:
> On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:
> 
>> Hi,
>> 
>> Is it possible to restart tomcat from within itself?
>> 
>> I host a JSP website and somehow the memory utilization keeps on 
>> increasing from time to time. I then have to manually restart 
>> Tomcat.
>> 
>> While I understand that in the long term, I should focus on
>> fixing the memory leaks, is there a way by which I can restart
>> Tomcat after the free memory goes below a threshold percentage?
> 
> I wouldn't try to do this inside Tomcat.  With the tools provided
> by the JVM (jmap or JMX), you can monitor just as easily from
> outside Tomcat.  Instead, create a monitoring script to watch the
> Tomcat process.  When memory usage hits a certain level, dump the
> heap (so you can analyze your memory problem later) and restart
> Tomcat.

+1

If you want to chance it, you could set -XX:OnOutOfMemoryError=<cmd>
and write a script that tries to dump your heap (or just have the JVM
do that for you) and then bounce Tomcat.

Note that not all OOMEs are life-threatening for the JVM.

I have experienced two kinds of OOMEs:

1) Truly out of memory: your app simply needs more heap. In these
   cases, the JVM generally goes into GC panic for a short amount of
   time trying to free-up memory and then finally throws OOME.
   Nothing meaningful seems to work after that: DBCPs run dry,
   etc. In these cases, I have had to restart my JVM completely
   in order for any users to get anything meaningful done.

2. Transient memory exhaustion: this happens when we have some careless
   code that loads 100,000 records from a single database query all
   into a big java.util.List or something like that. In these cases,
   a single thread gets an OOME, the List gets GC'd very quickly, and
   the server (and JVM) are fine. We generally do restart our JVMs when
   this happens, but we can wait until off-hours when fewer users are
   likely to be impacted in any way.

> Alternatively, you could use a preexisting tool to monitor your 
> Tomcat instance like Nagios or Hyperic.

These solutions really only monitor. Yes, you can configure them to
"react" to certain situations by abusing the notification system, but
they are really meant for monitoring and notification, not for
automated tasks.

>> I am using tomcat 6.0.14 on unix.
> 
> This is really old, upgrade.

+10

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlEGmisACgkQ9CaO5/Lv0PAMeACfflakV2t8RT5kjF/wQf8joYl9
swgAmgL1pwbvX5yLe2jUSGaOXXr5SDzf
=MaHM
-----END PGP SIGNATURE-----

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


Re: Restarting Tomcat from within itself?

Posted by Daniel Mikusa <dm...@vmware.com>.
On Jan 26, 2013, at 5:19 AM, Sunil Shevante wrote:

> Hi,
> 
> Is it possible to restart tomcat from within itself?
> 
> I host a JSP website and somehow the memory utilization keeps on increasing from time to time. I then have to manually restart Tomcat.
> 
> While I understand that in the long term, I should focus on fixing the memory leaks, is there a way by which I can restart Tomcat after the free memory goes below a threshold percentage?

I wouldn't try to do this inside Tomcat.  With the tools provided by the JVM (jmap or JMX), you can monitor just as easily from outside Tomcat.  Instead, create a monitoring script to watch the Tomcat process.  When memory usage hits a certain level, dump the heap (so you can analyze your memory problem later) and restart Tomcat.

Alternatively, you could use a preexisting tool to monitor your Tomcat instance like Nagios or Hyperic.

> 
> I am using tomcat 6.0.14 on unix.

This is really old, upgrade.

Dan

> 
> Thanks,
> Sunil

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