Showing posts with label redhat. Show all posts
Showing posts with label redhat. Show all posts

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