2

I'm trying to set up YouTrack on OS X Lion Server and have it launch at boot using launchd. I have downloaded the YouTrack jar file which I can successfully run from the command line using java -Xmx512m -jar youtrack.jar 8080. I have also wrapped said run command in a shell script similar to this which basically lets me interact with the active instance using youtrack start and youtrack stop. It stores the process id of the java process in a file and the exits in order to be able to stop a running instance, allowing only one instance etc.

In order to run this command at boot time I have created a simple launchd plist and placed it in /Library/LaunchDaemons, as per the instructions given in man launchd.plist. The principal keys used are ProgramArguments (youtrack start) and RunAtLoad (true). The problem is, when run form the command line youtrack start will create a file containing the pid of the java process, and I can see this process in top or ps ax. But when the command is being run by lanuchd (sudo launchctl load /Library/LaunchDaemons/org.example.youtrack.plist) the resulting pid will not match that of any running processes, and the YouTrack web server wont start. It's almost as if the java process started by launchd will also terminate once launchd has finished.

However, if I instead simply put the java command directly in the launchd plist it will work fine. I would simply like to understand why this is - I thought the while idea of launchd was to start daemons. But does that mean that the daemon must run inside launchd for its entire lifetime? Is there an alternate way of starting daemons with (or without) launchd, that would allow me simply to run a command once at boot? Basically what we used to do using @roboot in cron in earlier versions of OS X.

1 Answer 1

6

Launchd doesn't just launch programs, it monitors them as they run. By default, it expects the programs to keep running (as daemons), not to start some other program (/background copy of themselves/whatever) and exit. If the program does exit, launchd does two things that can cause problems for a run-and-exit program like youtrack start: it'll "clean up" leftover subprocesses (like the actual server process), and (depending on some other settings) maybe retry the "failed" program. There are two ways to solve this:

  1. Do it the launchd way: have the launchd item run java -Xmx512m -jar youtrack.jar 8080 directly (note that each "word" of the command should be a separate element of the ProgramArguments array). If you add <key>KeepAlive</key><true/> to the launchd .plist, it'll restart the server if it crashes or exits for any reason (note that this includes being killed, so you should rewrite youtrack stop to use launchctl unload ... and let launchd do the killing).

  2. Tell launchd not to expect it to keep running, and not to kill off subprocesses. Add this to your .plist to turn off the default behavior:

    <key>KeepAlive</key>
    <false/>
    <key>AbandonProcessGroup</key>
    <true/>
    
1
  • Thanks, that's superb answer! I wish I could give you another upvote.
    – Simon
    Feb 7, 2013 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .