Q:

Start Jenkins slave on Mac OS X as LaunchDaemon

I'am trying to start the jenkins' slave.jar on a Mac OS X 10.8.3 using a Launch Daemon so that it runs right after the machine is started.
But I'm experiencing a mysterious problem:
The LaunchDaemon starts the slave as i can see in the log files
29.04.2013 14:57:06 hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://ci.****/]
29.04.2013 14:57:06 hudson.remoting.jnlp.Main$CuiListener status
INFO: Connecting to ci.****:53930
29.04.2013 14:57:06 hudson.remoting.jnlp.Main$CuiListener status
INFO: Handshaking
29.04.2013 14:57:06 hudson.remoting.jnlp.Main$CuiListener status
INFO: Connected

But the connection is closed right after that:
29.04.2013 14:57:06 hudson.remoting.SynchronousCommandTransport$ReaderThread run
SCHWERWIEGEND: I/O error in channel channel
java.io.IOException: Unexpected termination of the channel
    at hudson.remoting.SynchronousCommandTransport$ReaderThread.run(SynchronousCommandTransport.java:50)
Caused by: java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2576)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1295)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)
    at hudson.remoting.Command.readFrom(Command.java:92)
    at hudson.remoting.ClassicCommandTransport.read(ClassicCommandTransport.java:59)
    at hudson.remoting.SynchronousCommandTransport$ReaderThread.run(SynchronousCommandTransport.java:48)
29.04.2013 14:57:06 hudson.remoting.jnlp.Main$CuiListener status
INFO: Terminated

Here i found a solution to this problem - adding sudo as first command. 
This works when i run it by myself in Terminal OR when i manually stop & start the LaunchDaemon via launchctl. It sounds stupid - running a command which is executed via sudo but it works in this case. 
BUT it doesn't work when system starts manually. Any idea? Here's my LaunchDaemon.plist file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.ci</string>
    <key>ProgramArguments</key>
    <array>
        <string>sudo</string>
        <string>/usr/bin/java</string>
        <string>-jar</string>
        <string>/Developer/jenkins/slave.jar</string>
        <string>-jnlpUrl</string>
        <string>http://ci.****/computer/****/slave-agent.jnlp</string>
        <string>-jnlpCredentials</string>
        <string>user:apitoken</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/Developer/jenkins/stdout.log</string>
    <key>StandardErrorPath</key>
    <string>/Developer/jenkins/error.log</string>
</dict>
</plist>

A:

The problem is, that the jenkins slave tries to start some UI but this is - of course - not allowed. I found the same error which is related to an official Bug Report: https://issues.jenkins-ci.org/browse/JENKINS-15697
The solution is in this case to add -Djava.awt.headless=true to JVM options which leads to the following configuration of a LaunchDaemon.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.ci</string>
    <key>ProgramArguments</key>
    <array>
        <string>sudo</string>
        <string>/usr/bin/java</string>
        <string>-Djava.awt.headless=true</string>
        <string>-jar</string>
        <string>/Developer/jenkins/slave.jar</string>
        <string>-jnlpUrl</string>
        <string>http://ci.****/computer/****/slave-agent.jnlp</string>
        <string>-jnlpCredentials</string>
        <string>user:apitoken</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/Developer/jenkins/stdout.log</string>
    <key>StandardErrorPath</key>
    <string>/Developer/jenkins/error.log</string>
</dict>
</plist>

I've tested this and it works. 

