Running GlassFish on Mac OS X using launchd

I run a Mac Mini as my home server, currently with OS X 10.4. Tonight I dug around and got GlassFish up and running using OS X’s answer to SMF on Solaris, launchd. I am by no means an expert on launchd but I will happily document what worked for me. First off I installed GlassFish v2 to “/usr/local/glassfish” as per the documented instructions.

Once I verified that it all worked, I sought out how to integrate it into launchd. I certainly don’t want it running as root, so I took a look at the available users and groups using the NetInfo Manager application. I found an “appserver” user and associated “appserverusr” group. Seemed a good place to start. I changed the ownership recursively for my install directory to this user and group combination. Once done, I created my launchd plist file and saved it to “/Library/LaunchDaemons/glassfish.plist”.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
<plist version="1.0">

Similar to SMF you have to load/import the service description. On OS X you use “launchctl load ./glassfish.plist” for this purpose. If all goes well you will have a running instance of GlassFish. If not, you will see rather vague error messaging in the system.log file.

Some drawbacks. It seems that launchd relies on sending a SIGTERM to the process when you attempt to stop it using “launchctl stop com.sun.glassfish”. This doesn’t work for GlassFish. I prefer the ability of SMF to define a appropriate shutdown command. Not a huge deal as I can easily stop GlassFish using asadmin or the admin console when needed. Once the process is stopped you can use “launchctl start com.sun.glassfish” to bring it back up. There is no notion of a restarter if the process dies. I think there is a new KeepAlive element in OS X 10.5 but I haven’t be able to experiment with that version yet. Sounds promising though.

All in all this works pretty well. GlassFish will launch on boot using the recommended subsystem. If anyone has any tips on achieving a tighter integration with launchd let me know.


16 Responses to “Running GlassFish on Mac OS X using launchd”

  1. rama Says:

    you seem fairly coherent, all things considered. 🙂

  2. Matthew Says:

    Must keep the brain working. Plus this was all tied to getting baby pictures up on the server. 🙂

  3. Gert Thiel Says:

    Do you know Lingon ( It’s a GUI to launchd configurations.

  4. Matthew Says:

    Thanks for the pointer Gert. I will take a look.

  5. Per Ejeklint Says:

    Thanks for the post! But I can’t get it to work. It starts up but immediately dies and the only thing I get in the log is this:[146] (com.sun.glassfish[287]): Stray process with PGID equal to this dead job: PID 289 PPID 1 java
    It’s the same what ever I try…

  6. Matthew Says:

    Hi Per,
    I tested this on 10.4.11 and it has been working fine for me after many reboots. Are you running 10.5? I have yet to try it there.

  7. Per Ejeklint Says:

    Correct you are, Matthew. 🙂 I’m running 10.5.1. I also tried a clean installation of Leopard but it’s still the same. And Lingon didn’t create any more successful launchd scripts… oh well, will continue to fight later this week.

  8. Per Ejeklint Says:

    OK, I think I figured it out. Basically, launchd in Leopard will by default kill the processes in the process group that asadmin script creates. A quick and very dirty fix is to set the flag AbandonProcessGroup to true. But then you can’t stop the server with "launchctl unload" since launchctl doesn’t have a handle to the server process that asadmin created.
    There’s probably a smart and efficient fix for this but I just made a new plist file for launchd, which contains the actual invocation of java with LOTS of parameters, as they are specified by ps when having glassfish up and running. Works for me until I find a smarter way.

  9. Matthew Says:

    Interesting. I just upgraded my server to Leopard this past weekend. I still need to get glassfish going again and will reply if I can come up with a different solution than you posted. Thanks for the followup!

  10. Chris Says:

    FWIW I’m having problems with jetty and launchd under Leopard; works fine under Tiger. I’m wondering about the general support for java processes under launchd on Leopard – I’ll check back to see if you have luck here with Glassfish.

  11. Scott Says:

    I am having the same issues loading GlassFish via launchd in 10.5.1. The process loads then immediately dies with:[171] (com.sun.glassfish[315]): Stray process with PGID equal to this dead job: PID 317 PPID 1 java
    Additionally, I tried the suggestion of setting the AbandonProcessGroup flag to true and saw no change in the problem. I am by no means a launchd expert…I’m assuming this flag should be set in the plist file. I’ve looked around extensively for clues as to why this "Stray process" error comes up but found nothing helpful…it seems to pop up in various forums when a process crashes in 10.5.

  12. Scott Says:

    One quick additional note. I just upgraded my os to OS X 10.5.2 and saw no change in the problem.

  13. Chris Corbell Says:

    I am not certain but I think that launchd plist needs to be OWNED by root/wheel. This does not mean it has to run as root.
    Using chown root and chgrp wheel on my lunchd plist for jetty fixed the issue I was seeing on Leopard.
    From Apple TN 2083 (
    * When you install your daemon, make sure that you set the file system permissions correctly. Apple recommends that daemons be owned by root, have an owning group of wheel, and use permissions 755 (rwxr-xr-x) for executables and directories, and 644 (rw-r–r–) for files. In addition, every directory from your daemon up to the root directory must be owned by root and only writable by the owner (or owned by root and sticky). If you don’t do this correctly, a non-admin user might be able to escalate their privileges by modifying your daemon (or shuffling it aside).
    * Try to avoid running your daemon with elevated privileges (for example, running it as root). If your daemon must run with elevated privileges, do not trust data received from non-privileged processes. Doing so might allow a local user to escalate their privileges.
    Hope this works for glassfish as well!

  14. Per Ejeklint Says:

    I just wrote a little blog entry on how I got it to work. Feel free to comment/correct me if I want.

  15. Per Ejeklint Says:

    oops, if *you* want, I mean… 🙂

  16. Creating a GlassFish service on Mac OS X | Dave Koelmeyer Says:

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: