plist - "hello world"launchd plist 不工作

标签 plist launchd

我在 ~/Library/LaunchAgents 有以下 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.yogapo.test_launchd</string>

    <key>Program</key>
    <string>. /Users/luke/dev/data_yogapo/script/test_launchd.sh</string>

    <key>StartInterval</key>
    <integer>10</integer>

    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

test_launchd.sh 文件包含以下内容:
#! /bin/bash 

echo "hello world from test_launchd.sh" >> /Users/luke/dev/data_yogapo/log/development.log

当我手动运行 test_launchd.sh 时. /Users/luke/dev/data_yogapo/script/test_launchd.sh结果如预期:该行出现在 development.log 的末尾

但是当我加载这个 plist 文件时,没有任何 react :
$ cd ~/Library/LaunchAgents
$ launchctl load com.yogapo.test_launchd.plist
$ launchctl list | grep yogapo
  -       1       com.yogapo.test_launchd

我已经尝试过使用和不使用 RunAtLoad 键。我已经在 SO 以及互联网上的其他地方查看了其他答案。我已经按照教程进行了操作,但什么也没发生。非常感谢任何帮助 - 谢谢!

最佳答案

您在问 launchd运行程序调用

". /Users/luke/dev/data_yogapo/script/test_launchd.sh"

它将采用 Program 的全部值键作为 execvp 的第一个参数(请参阅 man execvp(3) 了解更多详情)

如果您要检查 system日志,你会看到类似的东西:

May 22 21:17:38 dented com.apple.launchd.peruser.501[202] (com.yogapo.test_launchd[32986]): posix_spawn(". /Users/luke/dev/data_yogapo/script/test_launchd.sh", ...): No such file or directory May 22 21:17:38 dented com.apple.launchd.peruser.501[202] (com.yogapo.test_launchd[32986]): Exited with exit code: 1


launchd不是 shell 。但是,它可以解释 hash-bang 字符序列来确定使用哪个程序来解释您的脚本。因此,只需将您的脚本指定为要运行的程序:
<key>Program</key>
<string>/Users/luke/dev/data_yogapo/script/test_launchd.sh</string>

注意:如果您需要向脚本传递参数,请使用 ProgramArguments键,并将整个命令行放在那里。例如:
<key>ProgramArguments</key>
<string>/Users/luke/dev/data_yogapo/script/test_launchd.sh arg1 arg2 arg3</string>

关于plist - "hello world"launchd plist 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10676252/

相关文章:

Python 子进程在 launchd 运行时不起作用

macos - 非常简单 Launchd plist 不运行我的脚本

macos - 通过 launchd 在关机时运行 Shell 脚本

macos - 如何调试不在启动时运行的 Launchd 脚本?

objective-c - launchd:Mach 服务查找失败

ios - NSPhotoLibraryAddUsageDescription 错误

ios - 在 plist 文件中使用用户定义的build设置

string - 从字符串创建 NSData 对象

ios - 写入plist文件而不写入

ios - 数据在 iOS 中以无序方式插入