launchd 给了我“没有这样的文件或目录”错误

Rad*_*dek 5 plist macos

我的 .plist 如下所示......在 /var/log.system.log 我可以看到

(com.example.exampled[24728]): posix_spawn("/usr/local/bin/ruby /Users/radek/Sites/sinatrasvn/web.rb", ...): No such file or directory
(com.example.exampled[24728]): Exited with exit code: 1
(com.example.exampled): Throttling respawn: Will start in 10 seconds
Run Code Online (Sandbox Code Playgroud)

但如果我运行/usr/local/bin/ruby /Users/radek/Sites/sinatrasvn/web.rb脚本工作正常。任何的想法?

       <?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>com.example.exampled</string>
    <key>ProgramArguments</key>
    <array>
                 <string>/usr/local/bin/ruby /Users/radek/Sites/sinatrasvn/web.rb</string>
    </array>
    <key>KeepAlive</key>
    <true/>
       </dict>
       </plist>
Run Code Online (Sandbox Code Playgroud)

gee*_*aur 10

launchd不使用 shell 来运行程序;它使用exec系统调用。这就是您上面的 plist 使用数组的原因。

您正在将该数组的单个元素设置为包含空格的字符串,这会导致launchd尝试exec("/usr/local/bin/ruby /Users/radek/Sites/sinatrasvn/web.rb")- 果然,这不是文件的名称。相反,您要设置数组:

    <array>
                 <string>/usr/local/bin/ruby</string>
                 <string>/Users/radek/Sites/sinatrasvn/web.rb</string>
    </array>
Run Code Online (Sandbox Code Playgroud)

这会将路径作为单独的参数传递给exec(),正确的事情就会发生。

  • 每次登录时都将自动加载 ~/Library/LaunchAgents 中的所有内容,因此只有当前登录会话才需要 `launchctl load ...`(或者您可以退出并重新登录)。顺便说一句,对于像这样的本地开发的启动项,您可能应该使用“本地”。而不是“com.example”。作为标签前缀。 (2认同)