OSX launchctl list 没有找到我的 plist

OSX launchctl list 没有找到我的 plist

我创建了包含以下内容的文件/System/Library/LaunchDaemons/com.rundeckd.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <true/>
    <key>Label</key>
    <string>com.rundeckd</string>
    <key>ProgramArguments</key>
    <array>
        <string>/test/rundeck/server/sbin/rundeckd</string>
        <string>start</string>
    </array>
    <key>KeepAlive</key>
    <true/>

    <key>StandardOutPath</key>
    <string>/test/rundeck/var/log/launchd_out.log</string>
    <key>StandardErrorPath</key>
    <string>/test/rundeck/var/log/launchd_sdd.log</string>
    <key>Debug</key>
    <true/>
</dict>
</plist>

但是,sudo launchctl list 没有显示这个 rundeckd。

为什么?

此外,rundeckd 不会在启动时运行它,也不会创建日志文件。(请注意,我已修改 rundeckd 以在脚本中硬编码所需的 $RDECK_BASE 环境变量)

更新

Gordon Davisson,我按照你所说的修改了 plist,并且在不修改 rundeckd 脚本的情况下,我得到了以下内容:

nohup: can't detach from console: Inappropriate ioctl for device

然后,我尝试向 rundeckd 添加选项“launchd”(目前有 start、stop 和 status),如下所示:

launchd() {
    echo "%s" "launchd $prog: "
    touch $LOK_FILE
    $rundeckd 2>&1 >>$RDECK_BASE/var/log/service.log &
    PID=$!
    echo $PID > $PID_FILE
    fg $PID  # block until it is stopped
}

但是我得到了错误fg: no job control,因为它不是一个交互式shell,所以我似乎无法运行“fg”。 https://stackoverflow.com/questions/11821378/what-does-bashno-job-control-in-this-shell-mean

答案1

您的 launchd plist 将该项目标记为已禁用:

    <key>Disabled</key>
    <true/>

为了使其运行,您需要将其更改为<false/>,或用覆盖它sudo launchctl load -w /System/Library/LaunchDaemons/com.rundeckd.plist-w使其执行永久覆盖)。

另外,您是否修改过脚本以避免将其自身守护进程化?如果没有,则需要更改<key>KeepAlive</key><false/>,然后添加<key>AbandonProcessGroup</key><true/>

顺便说一句,您还可以在 .plist 中定义环境变量,而不必修改脚本来包含它们:

    <key>EnvironmentVariables</key>
    <dict>
        <key>RDECK_BASE</key>
        <string>/Users/david/bin/rundeck</string>
    </dict>

最后,我建议更改标签和文件名——它们应该基于反向 DNS 命名约定,因此除非您拥有 rundeckd.com 域名,否则您不应将其用作标签或标签前缀。对于此类自制条目,我建议使用“本地”前缀,即“local.rundeckd”。

更新:作业控制仅在交互式 shell 中可用;使用wait $PID而不是fg $PID

答案2

https://gist.github.com/dportabella/7714449

$RDECK_BASE/server/sbin/rundeck_launchd是一个用于启动 rundeck 而不将进程发送到后台的脚本(OSX Launchd 需要)。

/System/Library/LaunchAgents/david.rundeck.plist是将 rundeck 设置为 Mac OSX launchd 服务的配置文件。您需要根据需要修改此文件。在此示例中,RDECK_BASE 为 /Users/david/bin/rundeck。

安装这两个文件后,重新启动计算机,rundeck 将在启动时启动。

然后您可以停止、启动并查看 rundeck 服务的状态:

$ launchctl start david.rundeck
$ launchctl stop david.rundeck
$ launchctl list david.rundeck

(该脚本基于$RDECK_BASE/server/sbin/rundeckd脚本)

文件$RDECK_BASE/server/sbin/rundeck_launchd

#!/bin/bash 
#
# rundeck_launchd    Startup script for the RunDeck Launcher install
#   paramaters:
#     - env vars: [RDECK_BASE, RDECK_PORT, RDECK_LAUNCHER]
#     - standard RDECK_PORT values: [http: 4440, https: 4443]

export RDECK_BASE=${0%/*/*/*}

# RDECK_BASE must be set and exist
[ -z "$RDECK_BASE" -o ! -d "$RDECK_BASE" ] && {
    echo "RDECK_BASE not set or does not exist" ;
    exit 1 ;
}

# Source installation profile
. $RDECK_BASE/etc/profile

# Get the Launcher Jar path
[ -z "$RDECK_LAUNCHER" ] && {
    # Defaults to location of first startup
    RDECK_LAUNCHER=$(ls $RDECK_BASE/rundeck-launcher-*.jar)
}
[ -r "$RDECK_LAUNCHER" ] || {
    echo "RDECK_LAUNCHER not found: $RDECK_LAUNCHER" 
    exit 1;
}

# lookup the server port from the tools config file
RDECK_PORT=`awk '/framework.server.port/ {print $3}' $RDECK_BASE/etc/framework.properties`

# set the ssl opts if https is configured
SSL_OPTS=
proto=$(awk '/framework.server.url = / {split($3, a, ":"); print a[1]}' $RDECK_BASE/etc/framework.properties)
[ "${proto:-http}" == "https" ] && {
   SSL_OPTS="-Drundeck.ssl.config=$RDECK_BASE/server/config/ssl.properties"
}

rundeckd="${JAVA_HOME}/bin/java ${RDECK_JVM} -Dserver.http.port=${RDECK_PORT:=4440} $SSL_OPTS -jar ${RDECK_LAUNCHER}"

[ -w $RDECK_BASE/var ] || {
    echo "RDECK_BASE dir not writable: $RDECK_BASE" 
    exit 1 ;
}

mkdir -p $RDECK_BASE/var/run
mkdir -p $RDECK_BASE/var/log
mkdir -p $RDECK_BASE/var/lock/subsys

$rundeckd 2>&1 >>$RDECK_BASE/var/log/service.log

文件/System/Library/LaunchAgents/david.rundeck.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>david.rundeck</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/david/bin/rundeck/server/sbin/rundeck_launchd</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>UserName</key>
    <string>david</string>
    <key>Debug</key>
    <true/>
</dict>
</plist>

相关内容