Automatically start GlassFish 3.1.2 on Oracle Linux 6.2 after server reboot

Environment: GlassFish 3.1.2 Open Source Edition (Web Profile), Oracle Linux 6.2

When you installed GlassFish on Oracle Linux, you probably want to make sure that your Domain Administration Server (DAS) and/or any standalone instances that you created are automatically started after a server reboot. You can do this with a shell script which you add as a service under Linux.

The first step is to create a script under /etc/init.d (with user root):

$ vi /etc/init.d/glassfish


# description: glassfish start stop restart
# processname: glassfish
# chkconfig: 2345 20 80


case "$1" in
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-local-instance instance1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-local-instance instance1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin restart-local-instance instance1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin restart-domain domain1"
echo $"Usage: $0 {start|stop|restart}"
exit $RETVAL

This script will start, stop or restart the DAS with name “domain1” and the local instance with name “instance1” on your Linux server. You should adapt the script for your specific GlassFish instances.

GLASSFISH_HOME is the folder where GlassFish was installed (in my case: /u01/app/glassfish3). GLASSFISH_USER is the name of the user account under which GlassFish is running (in my case: user oracle).

Note: GlassFish needs a JAVA_HOME variable pointing to the folder of your Java binaries. In my case, JAVA_HOME is defined in the .bash_profile of the user “oracle”. You could also add it to the /etc/init.d/glassfish script.

Next, set the correct file permissions on the script:

$ chmod 755 /etc/init.d/glassfish

You can test the script as follows:

$ sh /etc/init.d/glassfish start

Now we need to create a service from the script:

$ chkconfig –add glassfish

This will create a service and start it in the runlevels 2,3,4 and 5. To check this, run the following command:

$ chkconfig|grep glassfish
glassfish 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Let’s check the service:

$ service glassfish stop

Waiting for the instance to stop …
Command stop-local-instance executed successfully.
Waiting for the domain to stop ….
Command stop-domain executed successfully.

Looks ok! The last thing you should test is rebooting your server to see if GlassFish is automatically started after the reboot. Have fun!



One Response to Automatically start GlassFish 3.1.2 on Oracle Linux 6.2 after server reboot

  1. chkconfig|grep glassfish

    Nota: Esta salida muestra solo servicios SysV y no incluye servicios nativos de systemd. Los datos de configuración SysV puede ser sobre escritos por configuración nativa de systemd.
    Si desea una lista de servicios systemd use ‘systemctl list-unit-files’.
    Para ver los servicios activados en un destino particular use
    ‘systemctl list-dependencies [target]’.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: