Automatic Starting of Servers in GlassFish 3.1

Note: At the end of this blog are links to older blogs from V3.0 about this feature.  You may want to look them over as well.

The way we offer the ability to automatically start GlassFish servers is via "services" on the platform.  "Services" are an ancient technology, available on all of our supported platforms (and I would imagine is available on every serious OS).  Services allow applications to run automatically when a computer boots up.  Nobody needs to login -- the service just starts automatically.  A simple example of this is the FTP server.  When you boot up your computer you can access FTP from any other computer without touching the FTP computer.  This is very important in the event of a power failure.  On Windows you can setup automatic security updates and Windows will automatically reboot your computer.  If you have a Subversion server that is not configured as a service and you are halfway around the world depending on it -- you will become a Services Specialist in the near future when you return home!

GlassFish 3.1 Platform Services now supports all Linux versions, Windows and Solaris10/SMF

Services can be quite complex.  For instance you might want to have it do this:

  1. Automatically start a GlassFish server upon booting
  2. If the server crashes (no way!) restart it
    What if it crashes every time?  The machine will be in trouble with an infinite loop so:
  3. Try to restart 3 times and then give up.

Setting up this level of services-granularity is very difficult and expensive to implement perfectly.  For one - we are supporting several platforms and they all do these things completely differently. The hard part is setting up services at all to do the basic things.  Once that is complete you can easily use the platform's native tools for adjusting things just the way you like.  What we support is the classic services model -- which is to start the service upon booting, unattended.

I worked hard to make it easy to setup services.  Here's how you would create them for an installation that has one Domain:

asadmin create-service

If you want to make sure it would work but you aren't yet ready to pull the trigger, or you just want to see what it would do -- then run this command:

asadmin create-service --dry-run

If your domain is named, say. domain1 then the service's name will be domain1.  Simple.

 After the service is created you are greeted with a platform-specific message giving you the details on how to start the service.  We do NOT automatically start it.  I personally recommend rebooting immediately -- and it will start.

Windows is particularly easy and flexible to work with.  You can manage the GlassFish service(s) by:

  1. GUI -- right-click on Computer/Manage/Services
  2. sc.exe  very handy tool for querying/starting/stopping/restarting etc.
  3. In the bin directory of the domain or instance run the your-service-nameService.exe application that we put in there. 

How to setup automatic starting for an instance?

Easy!  Simply give the name of the instance as the final argument to create-service.  The following command will create a service for myinstance:

asadmin create-service myinstance

There is an undocumented, unsupported command that will delete a service for you.  It is very very easy to delete a service on SMF and Windows.  On Linux it can be tedious.  I've personally tested the command successfully but it has not been through the merciless testing of QA at Oracle.  Use it at your own risk.

asadmin _delete-service 

Important blog for making sure GlassFish doesn't stop when you logout.

I have another blog with implementation details if you are interested.

Some older blogs that pertain to V3.0:

V3.0 Platform Services for Windows and SMF

DIY Cookbook for Configuring Linux Services

How to Run on Linux as a Non-Root User


Thanks a lot, this worked beautifully for me on AWS EC2 Linux with very little linux ability.

I have installed a JDK (1.6.0_26) but am finding that glassfish appears to be being started with the JRE that's installed in the linux image by default.

I have modified /etc/profile to include at the end:

export PATH

which means that java -version at the console uses the correct version.

For some reason though the service doesn't appear to use this version.

Any suggestions you could offer would be much appreciated.

Posted by guest on June 21, 2011 at 09:42 AM PDT #

Hello Byrons, thanks for this helpful post. I was doing something similar in GlassFish 2.1 so it was a relief to see that you had already implemented this in 3.1. However I'm having a problem creating a service for a particular instance. When I run: asadmin create-service --serviceuser glassfish instance1

It returns: No node parent directory found. Command create-service failed.

Do you know what could be causing this?

Posted by Hugo Garza on July 25, 2011 at 08:28 AM PDT #

It told me if I ran it it would be successful and instructed me to remove the "--dry-run" suffix and when I did, if compeletely failed spewing WMI errors all over the place.

Does it all work like this?

Posted by Lol on February 16, 2012 at 05:08 AM PST #

Works great (GF 3.1.2)! In my case, on a Windows 2008 Server, I needed to run asadmin in a command-line console with Administrator privileges ("Run as Administrator"). Otherwise the service will not be created.

Posted by Ramon Rockx on May 24, 2012 at 12:43 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed



« June 2016