在 Mac OS X 上以 LaunchDaemon 形式启动 Jenkins 从属服务器

在 Mac OS X 上以 LaunchDaemon 形式启动 Jenkins 从属服务器

我正在尝试使用启动守护程序在 Mac OS X 10.8.3 上启动 jenkins slave.jar,以便它在机器启动后立即运行。

但我遇到了一个神秘的问题:LaunchDaemon正如我在日志文件中看到的那样,从服务器启动了

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

但此后连接立即关闭:

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

这里我找到了解决这个问题的方法——添加sudo为第一个命令。

当我在终端中自己运行它或当我通过手动停止和启动 LaunchDaemon 时,此方法有效launchctl。这听起来很愚蠢 - 运行通过 sudo 执行的命令,但在这种情况下它可以工作。 没有在系统手动启动时工作。有什么想法吗?这是我的 LaunchDaemon.plist 文件:

<?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>

答案1

问题是,jenkins 从属尝试启动一些 UI,但这当然是不允许的。我发现了与官方 Bug 报告相关的相同错误:https://issues.jenkins-ci.org/browse/JENKINS-15697

在这种情况下,解决方案是添加-Djava.awt.headless=trueJVM 选项,从而导致 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>

我已经测试过并且它有效。

相关内容