Mac launchDaemon 服务反复启动我的 bash 脚本

问题描述

我一直在设置一个启动守护进程,它根据需要启动一个 bash 脚本。但是,一旦我加载了启动代理并通过套接字进行通信,它就会每 10 秒继续运行我的 bash 脚本。我什至在 plist 中将 KeepAlive 标志设置为 false 并在 bash 脚本中添加了延迟,但它仍然继续运行。>

如何将其设置为仅在与套接字连接时运行一次。

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.test.helper</string>
        <key>Program</key>
        <string>/Library/Application Support/test/testexec</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Library/Application Support/test/testexec</string>
        </array>
        <key>ondemand</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
        <key>Sockets</key>
        <dict>
            <key>MasterSocket</key>
            <dict>
                <key>SockFamily</key>
                <string>Unix</string>
                <key>SockPathMode</key>
                <integer>438</integer>
                <key>SockPathName</key>
                <string>/var/run/com.example.test.helper.socket</string>
                <key>SockType</key>
                <string>Stream</string>
            </dict>
        </dict>
    </dict>
</plist>

bash 脚本文件

#!/bin/bash
FILE_PATH="/var/log/test.log"


print()
{
    echo "$(date +"%m-%d %T")    $1" >> $FILE_PATH
}


/bin/rm -r "/private/var/test"
retcode=$?  
print "rm returncode: $retcode"

if [ $retcode -ne 0 ];
then
print "The removing Failed"
fi

sleep 15

解决方法

Mac OS X 10.4 Tiger 中使用“按需”键来控制作业是否保持活动状态。不应再使用该密钥。

keepalive 键设置为 false,默认为仅按需启动作业。