【发布时间】:2021-06-11 15:49:44
【问题描述】:
我一直在设置一个启动守护程序,它可以按需启动一个 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
【问题讨论】:
-
我怀疑这可能与它是 bash 脚本而不是常规可执行文件有关。您是否尝试过将
/bin/bash指定为守护程序并将脚本路径作为参数传递? -
@pmdj 你说的我已经试过了,它仍在重新启动 bash 脚本。
标签: macos plist launchd launchctl launch-daemon