Wednesday, October 8, 2014

Automatic glassfish startup in CentOS 7/RedHat Enterprise Linux 7 (systemd)

I tried using the bundled create-service command on the asadmin but there are some issues with it. Since it's probably meant to be used on different linux distributions, it has the following limitations:


  1. Does not support systemd out of the box
  2. Application server must run under root account
So, this is the script I wrote (you need to place it on /etc/rc.d/init.d/glassfish and give it proper execution permission):


#!/bin/sh -
# Script to start/stop/restart GlassFish
# description: glassfish
#

if [ -z ${GLASSFISH_HOME+x} ]; then
GLASSFISH_HOME=/opt/glassfish4;
fi


if [ -z ${GLASSFISH_USER+x} ]; then
GLASSFISH_USER=glassfish;
fi

if [ -z ${GLASSFISH_DOMAIN+x} ]; then
GLASSFISH_DOMAIN=domain1;
fi

GH=$GLASSFISH_HOME
GU=$GLASSFISH_USER
GD=$GLASSFISH_DOMAIN

case "$1" in
start)
su - $GU ${GH}/bin/asadmin start-database
su - $GU ${GH}/bin/asadmin start-domain 
;;
stop)
su - $GU ${GH}/bin/asadmin stop-database
su - $GU ${GH}/bin/asadmin stop-domain $GLASSFISH_DOMAIN
;;
restart)
su - $GU ${GH}/bin/asadmin stop-domain $GLASSFISH_DOMAIN
su - $GU ${GH}/bin/asadmin start-domain $GLASSFISH_DOMAIN
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

Then you need to do:

# systemctl --system daemon-reload
# systemctl enable glassfish
# systemctl start glassfish

And you are done.

Of course, make sure you have the right values on the environment variables. For that purpose I place a glassfish.sh file under /etc/profile.d/ with the following content:

GLASSFISH_HOME=/opt/glassfish4
GLASSFISH_USER=glassfish
GLASSFISH_DOMAIN=domain1
export GLASSFISH_HOME GLASSFISH_USER GLASSFISH_DOMAIN


2 comments:

  1. the only way I managed to get glassfish 3 running automatically after centOS7 boot is to run the command line :
    /usr/local/glassfish3/glassfish/bin/asadmin create-service

    ReplyDelete
    Replies
    1. Yes! I tried to create a exec for systemd, but was impossible. So, I agree

      Delete