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
    3. unzip
    4. rm
  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)"


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?

Posted by Straun on March 02, 2010 at 07:43 PM PST #

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.

Posted by W Byron Nevins Jr on March 03, 2010 at 05:29 AM PST #

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

Posted by Pango on March 10, 2010 at 11:48 PM PST #

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

Posted by W Byron Nevins Jr on March 11, 2010 at 01:34 AM PST #

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.

Posted by Pango on March 11, 2010 at 04:45 PM PST #

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

Posted by Pango on March 11, 2010 at 05:51 PM PST #

does have any problem if i installed glassfish in: /usr/local ?
cuz the glassfish came together with netbeans...


Posted by Broz on August 04, 2010 at 12:50 AM PDT #

You'll also need :
chmod +x glassfish

Posted by Alex on August 30, 2010 at 06:32 PM PDT #

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?

Posted by Lucia on January 30, 2011 at 06:13 PM PST #

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

Posted by Eric Tucker on April 03, 2011 at 04:26 PM PDT #

Thanks, also works for fedora.

Posted by Jorge Gallas on April 25, 2011 at 07:01 AM PDT #

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?

Posted by guest on May 10, 2011 at 03:02 PM PDT #

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.


Posted by guest on August 28, 2011 at 10:45 AM PDT #

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.

Posted by guest on November 10, 2011 at 05:58 PM PST #

your startup script is missing chkconfig comments :

for example :

#! /bin/sh
# chkconfig: 2345 99 01
# description: GlassFish Server

Posted by levancho on January 14, 2012 at 05:42 AM PST #

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

Posted by Rishi Trivedi on March 30, 2012 at 10:22 PM PDT #

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............

Posted by guest on July 04, 2012 at 12:26 AM PDT #

Step 4:

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

please help.

Posted by Ravi on July 08, 2012 at 02:21 AM PDT #

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

Posted by Byron Nevins on July 17, 2012 at 11:52 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed



« February 2017