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

Comments:

Nice step by step
Ah if only gf could run on my wii. ..

Posted by wadael on March 04, 2010 at 04:32 AM PST #

Welcome to the Pet products world.

Posted by Pet Products on March 04, 2010 at 03:34 PM PST #

userful information

Posted by Abercrombie on March 05, 2010 at 10:48 AM PST #

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

Posted by ed hary on March 05, 2010 at 11:12 AM PST #

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.

Posted by tiffany wholesale on March 05, 2010 at 11:14 AM PST #

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

Posted by ed hardy swimwea on March 05, 2010 at 11:15 AM PST #

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

Posted by Ignacio on August 10, 2010 at 08:32 AM PDT #

Test

Posted by guest on September 30, 2011 at 07:15 PM PDT #

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.

Posted by ugg boots on sale on November 07, 2011 at 05:56 PM PST #

If you dont want to use iptables, take a look here:
http://i-netsource.de/wordpress/?p=228

Posted by Joerg on December 30, 2011 at 04:30 AM PST #

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)"

Posted by guest on January 07, 2012 at 08:30 PM PST #

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)"

Posted by jm009 on January 07, 2012 at 08:32 PM PST #

Thanks for the step by step

Posted by aldemon on July 17, 2012 at 11:35 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

ByronNevins

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today