Coming Up for Air

Run GlassFish V3 As a Non-Root Service on Gentoo Linux

Thursday, March 04, 2010 |

Byron Nevins, a colleague of mine here at Oracle, has a couple of nice blog entries showing how to run GlassFish as a service, both as root and non-root users, on Ubuntu or Debian. As a Gentoo user, that doesn’t help me much, unfortunately, but, some time ago, I developed a script that works great for me, so I thought I’d share it here.

To make things a bit easier, go read Byron’s non-root user entry (changing the install location to /opt/glassfish unless you want to edit the script below), and stop at step 5. I’ll wait. All done there? Ok, great!

As the root user (or a user with appropriate privileges), create the file /etc/init.d/glassfish and paste the following script into it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/sbin/runscript
depend() {
        need net
}
start() {
        ebegin "Starting glassfish"
        # Ensure that we run from a readable working dir, and that we do not
        # lock filesystems when being run from such a location.
        cd /
        start-stop-daemon --start --quiet --background --make-pidfile --pidfile /var/run/glassfish.pid --exec /usr/local/bin/gf_start
        eend $?
}
stop() {
        ebegin "Stopping glassfish"
        /usr/local/bin/gf_stop
        eend $?
}
restart() {
        if ! service_stopped "${SVCNAME}" ; then
                svc_stop || return "$?"
        fi
        svc_start
}

I actually use a couple of scripts, as you might see there, to help manage my GlassFish instance, as I run this on my "dev" box. Those two scripts are /usr/local/bin/gf_start:

1
2
#!/bin/bash
su -c '/opt/glassfish/server/bin/asadmin start-domain' glassfish

and /usr/local/bin/gf_stop:

1
2
#!/bin/bash
su -c '/opt/glassfish/server/bin/asadmin stop-domain' glassfish

Once those files are in place (and paths are updated), test the script (/etc/init.d.glassfish start) to make sure everything is working as expected. Once you’re happy with it, you can register the service to start at system boot by issuing the command rc-update add glassfish default. The next time your system starts, GlasssFish will be patiently waiting for you on port 8080.

If you’d like, you can now issue the iptables commands that Bryon lists to redirect port 80 to 8080. I haven’t done that because I actually front GlassFish with Apache as I have other sites listening on 80 and uses virtual hosts to sort them all out, but that’s another topic altogether. :)

I hope that helps.

Search

    Quotes

    Sample quote

    Quote source

    About

    My name is Jason Lee. I am a software developer living in the middle of Oklahoma. I’ve been a professional developer since 1997, using a variety of languages, including Java, Javascript, PHP, Python, Delphi, and even a bit of C#. I currently work for Red Hat on the WildFly/EAP team, where, among other things, I maintain integrations for some MicroProfile specs, OpenTelemetry, Micrometer, Jakarta Faces, and Bean Validation. (Full resume here. LinkedIn profile)

    I am the president of the Oklahoma City JUG, and an occasional speaker at the JUG and a variety of technical conferences.

    On the personal side, I’m active in my church, and enjoy bass guitar, running, fishing, and a variety of martial arts. I’m also married to a beautiful woman, and have two boys, who, thankfully, look like their mother.

    My Links

    Publications