X
  • Sun
    March 4, 2010

Run GlassFish V3 as a non-root Service on Linux Ubuntu/Debian

Background

I already showed you how to setup GlassFish V3 as a Service on Linux here.  In that blog I used root as the GlassFish "owner" for simplicity.  In this blog we will do the same thing except with a non-root user.  If you already have a user and/or you have V3 installed you can skip some of the earlier steps.  Once again my main motivation is to get these procedures worked out carefully so that I can implement it automatically.  Currently we support Windows and Solaris 10.  For each step I'll also show you how to undo the change in case you want to rollback everything.

1. Create a user

I decided to create my own GlassFish user that exists solely to run GlassFish.  You could use your own existing account as well.  I also created a new group for use only by this user.  I.e. I don't want this user to have any Linux admin privileges.

sudo groupadd glassfish
sudo useradd -s /bin/bash -d /home/glassfish -m -g glassfish glassfish 

UNDO:

sudo userdel glassfish
sudo groupdel glassfish

2. Login as the new user in a terminal window and set a password (if desired)

sudo passwd glassfish
sudo -i -u glassfish


3. Install GlassFish V3 using user glassfish

cd ~
wget  http://download.java.net/glassfish/v3/release/glassfish-v3.zip
unzip glassfish-v3.zip
rm glassfish-v3.zip

UNDO:

rm -rf ~/glassfishv3

 

4. Exit the shell from step 2

 

5. Copy the script to /etc/init.d  and configure it as a Service

sudo cp the-script /etc/init.d/glassfish
sudo update-rc.d glassfish defaults

UNDO:

sudo rm /etc/init.d/glassfish
sudo update-rc.d glassfish remove

All Done

You can start, stop or restart GlassFish V3 like so.  Note that you must do it with an account that has admin privileges.  The user "glassfish" ironically can not do this even though the server will run as "his" process.

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

How to Allow GlassFish V3 to use ports less than 1024

 This technique configures the firewall to send all port 80 traffic to port 8080.  You can do the same thing for any other ports you need that are less than 1024.  I don't know how to undo it.

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080

Service Script

#!/bin/sh
# Platform Services for GlassFish
#

GF_USER=glassfish
GF_HOME=/home/$GF_USER/glassfishv3/glassfish
ASADMIN=$GF_HOME/bin/asadmin
SU="su --login  $GF_USER --command  "

case "$1" in
start)

    $SU "$ASADMIN start-domain > /dev/null 2>&1 &"

    ;;

stop)

    $SU "$ASADMIN stop-domain > /dev/null 2>&1 &"

    ;;

restart)

    $SU "$ASADMIN restart-domain > /dev/null 2>&1 &"

    ;;

\*)
    echo "usage: $0 (start|stop|restart|help)"

esac

Join the discussion

Comments ( 13 )
  • wadael Thursday, March 4, 2010

    Nice step by step

    Ah if only gf could run on my wii. ..


  • Pet Products Thursday, March 4, 2010

    Welcome to the Pet products world.


  • Abercrombie Friday, March 5, 2010

    userful information


  • ed hary Friday, March 5, 2010

    I had my first beacon encounter on Fandango and agree that the experience was troubling


  • tiffany wholesale Friday, March 5, 2010

    The average selling cost for World Tour was $126. What was the average selling cost of the GH3 units? If you're dealing with bundles you guys need to compare apples to apples.


  • ed hardy swimwea Friday, March 5, 2010

    Now, the release of the raw October NPD data allows for the tabulation of the two games' total sales. Unsurprisingly, Guitar Hero World Tour,


  • Ignacio Tuesday, August 10, 2010

    Hello,

    I must be missing something but when I start the service it runs as root user. Thanks for any ideas... and thanks for this text.

    Ignacio


  • guest Saturday, October 1, 2011

    Test


  • ugg boots on sale Tuesday, November 8, 2011

    It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.


  • Joerg Friday, December 30, 2011

    If you dont want to use iptables, take a look here:

    http://i-netsource.de/wordpress/?p=228


  • guest Sunday, January 8, 2012

    Thank you for the script.

    I added before the 'case "$1" in':

    alreadyRunning=`/bin/ps -Alf|/bin/grep java|/bin/grep glassfish|/bin/grep "com.sun.enterprise.glassfish.bootstrap.ASMain -domainname domain1 -asadmin-args --host,,,localhost,,,--port,,,4848"|/bin/grep ",,,start-domain,,,"|/bin/grep -v grep|/usr/bin/awk '{print $4}'`

    And I added before the '\*)':

    status)

    if [ -z ${alreadyRunning} ]

    then

    /bin/echo "Glassfish not running."

    else

    /bin/echo "Glassfish is running."

    fi

    exit $?

    ;;

    And I changed the line

    echo "usage: $0 (start|stop|restart|help)"

    to

    echo "usage: $0 (start|stop|restart|status|help)"


  • jm009 Sunday, January 8, 2012

    Thank you for the script.

    I added before the 'case "$1" in':

    alreadyRunning=`/bin/ps -Alf|/bin/grep java|/bin/grep glassfish|/bin/grep "com.sun.enterprise.glassfish.bootstrap.ASMain -domainname domain1 -asadmin-args --host,,,localhost,,,--port,,,4848"|/bin/grep ",,,start-domain,,,"|/bin/grep -v grep|/usr/bin/awk '{print $4}'`

    And I added before the '\*)':

    status)

    if [ -z ${alreadyRunning} ]

    then

    /bin/echo "Glassfish not running."

    else

    /bin/echo "Glassfish is running."

    fi

    exit $?

    ;;

    And I changed the line

    echo "usage: $0 (start|stop|restart|help)"

    to

    echo "usage: $0 (start|stop|restart|status|help)"


  • aldemon Wednesday, July 18, 2012

    Thanks for the step by step


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