• Sun
    February 25, 2010

How to Run GlassFish V3 as a Service on Linux Ubuntu/Debian

We already support running GlassFish V3 as a service on Solaris 10 and Windows platforms (see my blog).  I have been investigating how to provide support for automatically starting GlassFish V3 as a service on Linux.  Of course before I can hope to do that -- I must be able to set it up manually.  In this blog I will take you through the manual steps needed to run GlassFish V3 as a service on Linux.

This procedure was worked out on my Linux system which happens to be Ubuntu.  Other flavors of Linux may have slightly different procedures.

One decision you need to make right up front is what Linux user should "own" GlassFish V3.  Typically root is used as the owner.  If you choose root as the user you get the advantage that you can use ports < 1024 without complex configuration changes to the system.  For this blog I used root.

 Here are the steps  -- the file named "glassfish" in step 4 is the simple init script which appears at the end of this blog.

  1. Install JDK 6 if needed
  2. Have root install GlassFish like so:
    1. cd /opt
    2. wget  http://download.java.net/glassfish/v3/release/glassfish-v3.zip
    3. unzip glassfish-v3.zip
    4. rm glassfish-v3.zip
  3. cd /etc/init.d
  4. cp glassfish .
  5. update-rc.d glassfish defaults
  6. OPTIONAL /etc/init.d/glassfish start
  7. OPTIONAL Make sure GlassFish is running OK
  8. reboot -- you are done!

To start, stop, restart GlassFish simply run these commands:

sudo /etc/init.d/glassfish start
sudo /etc/init.d/glassfish stop
sudo /etc/init.d/glassfish restart


# glassfish init script for Linux
# Simplest possible case -- no password file, one default domain
# it would be simple to add such options


case "$1" in
    $GLASSFISH_HOME/bin/asadmin start-domain >/dev/null
    $GLASSFISH_HOME/bin/asadmin stop-domain >/dev/null
    $GLASSFISH_HOME/bin/asadmin restart-domain >/dev/null
    echo "usage: $0 (start|stop|restart|help)"

Join the discussion

Comments ( 19 )
  • Straun Wednesday, March 3, 2010

    Ideally, as you point out, you should not run the glassfish process as root and chroot limit the process to the installation folder too. And rebooting is for other operating systems. You wouldn't give someone a naive code example, so why provide a naive installation example?

  • W Byron Nevins Jr Wednesday, March 3, 2010

    1. The reboot is to prove that the service is setup and will start automatically. Of course you don't HAVE to do that. I personally wouldn't want to assume that it will work rather than making sure.

    2. The next blog (coming this week) will show EXACTLY how to setup using a non-root account. Of course it is way more complicated. That's why I started with this blog to avoid all the extraneous complexity dealing with creating a user account, etc.

  • Pango Thursday, March 11, 2010

    Step 4. (cp glassfish .) does not work for me.

    I followed your instructions but when i hit:

    root@my_machine:/etc/init.d# cp glassfish .

    My Machine says this:

    cp: cannot stat `glassfish': No such file or directory

  • W Byron Nevins Jr Thursday, March 11, 2010

    You need to create a file (I named it glassfish) and paste in the script from the end of this blog. Then copy that script file to /etc/init.d

  • Pango Friday, March 12, 2010

    Ah ok, now it makes sense.

    So now I got Glassfish up and running.

    Thanks for your good Tipps here so far. :D

    What I'm still missing is a easy way to start glassfish with a changing directory: /opt/my_rails-app/current/

    I deploy to that directory with capistrano more than once a day and to be honest i don't want to configure a new glassfish domain for every new step of deployment. That simply takes to long in a fast deployment process like the one capistrano is offering.

    I'm searching for the easiest way to get my webservice running with glassfish. Maybee i didn't get the whole "sense and meaning" of configuring glassfish domains for that issue, but it seems to be ways to complicated.

  • Pango Friday, March 12, 2010

    ... also changing the glassfish debug method from development to production mode in a dynamic way would be very interesting.

  • Broz Wednesday, August 4, 2010

    does have any problem if i installed glassfish in: /usr/local ?

    cuz the glassfish came together with netbeans...


  • Alex Tuesday, August 31, 2010

    You'll also need :

    chmod +x glassfish

  • Lucia Monday, January 31, 2011

    Glassfish v22 starts on boot nicely using your script, thanks. But admin console doesn't load, it displays the log-in screen, and after successful login it keeps loading forever. If I start the script after I login to machine, and not on boot, then admin console is working fine. I am using Ubuntu 10.04. Any idea?

  • Eric Tucker Sunday, April 3, 2011

    CentOS (and likely other Redhat/Redhat offshoots): If you'd like to make it manageable by chkconfig, add this at the bottom of the /etc/init.d/glassfish file:

    # chkconfig: 2345 20 80

    # description Glassfish v3.

    (Straight out of the bottom of the chkconfig manpage.)

    Now, instead of update-rc.d, you should do:

    chkconfig --add glassfish


    chkconfig glassfish on

  • Jorge Gallas Monday, April 25, 2011

    Thanks, also works for fedora.


  • guest Tuesday, May 10, 2011
    everytime i type 'sudo /etc/init.d/glassfish start' it gives an error..
    like this:
    '/etc/init.d/glassfish: command not found'
    why is that?
  • guest Sunday, August 28, 2011

    I had 2 problems, first I copied and pasted the script code in gedit, save it in /etc/init.d/, to execute step 5 he replied:

    #insserv: warning: script 'K01glassfish' missing LSB tags and overrides

    #innserv: warning: script 'glassfish' missing LSB tags and overrides

    The second mistake was when I tried to run the command manually:

    #./asadmin start-domain >/dev/null


    #Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS

    Do not install JDK 6 because I thought that is not necessary. What I can do? Is urgent to have this service on my server. Thank you very much for the help they can give me.


  • guest Friday, November 11, 2011

    This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work.

  • levancho Saturday, January 14, 2012

    your startup script is missing chkconfig comments :

    for example :

    #! /bin/sh

    # chkconfig: 2345 99 01

    # description: GlassFish Server

  • Rishi Trivedi Saturday, March 31, 2012


    I have installed Eclipse and glassfish server separately on my Ubuntu..

    Both are working fine but when i tried to add glassfish on run time environment in eclipse i am unable to do so.

    it is giving error "Missing classpath entry /modules/glassfish.jar"

    I tried to give path for the same which is /var/lib/glassfish3/glassfish/modules/

    this path contains glassfish.jar.

    but it is giving error "not valid installtion directory"

    how to resolve this prob?

    thanx in advance

  • guest Wednesday, July 4, 2012

    Does not work....Glassfish starts and can get to webpage..But if I click on any web page button for my visual jsf project, I get a faces exception. I don't get errors like this if I either startup glassfish directly from terminal or from Netbeans............Of course as soon as I close the terminal....glassfish shutdown............

  • Ravi Sunday, July 8, 2012

    Step 4:

    cp glassfish .

    cp: `glassfish' and `./glassfish' are the same file

    please help.

  • Byron Nevins Tuesday, July 17, 2012

    Here are the steps -- the file named "glassfish" in step 4 is the simple init script which appears at the end of this blog.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.