运行 launchd 中的 postgres 查询

Running postgres query in launchd

我正在尝试 运行 在 OSX Sierra 上定期进行 postgres 查询,我发现了我应该使用 launchd 的建议。我创建了一个调用包含 postgres 查询的简单 shell 脚本的 plist 文件,将 plist 文件复制到 LaunchAgents 文件夹中,然后加载它。但是,它似乎没有用。

如果我 运行ning 的 shell 脚本很简单,例如:

echo "Hello" > /Users/agentzel/Documents/temp.txt

它工作得很好——每 30 秒,它用 "Hello" 这个词刷新那个文件。但是,如果是像下面这样的 postgres 查询,我不会得到任何输出。

psql -U agentzel -d dvdrental -c 'select count(*) from film;' > /Users/agentzel/Documents/temp.txt

当我从命令行 运行 时,这两个命令都工作得很好,但是 postgres 查询在被 LaunchAgent 调用时不起作用。我对 postgres 或 launchd 知之甚少,所以如果能深入了解我做错了什么,我将不胜感激。

如果相关,这是我的启动文件:

<?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.test.database_info_sample</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/agentzel/Documents/test.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>30</integer>
</dict>
</plist>

(其中 test.sh 仅包含我正在尝试 运行 的命令)

编辑:如果没有人熟悉这个特定问题,是否有调试 launchd 的简单方法?如果 launchd 或 postgres 报告错误,是否有我可以检查错误的文件 message/code?

使用 Launchd 配置设置 STDOUT 位置,而不是 shell 脚本中的管道 (> ..txt)。

    <!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.test.database_info_sample</string>
       <key>ProgramArguments</key>
       <array>
         <string>/Users/agentzel/Documents/test.sh</string>
       </array>
       <key>StartInterval</key>
       <integer>30</integer>
       <key>StandardErrorPath</key>
       <string>/Users/agentzel/Documents/temp_err.txt</string>
       <key>StandardOutPath</key>
       <string>/Users/agentzel/Documents/temp.txt</string>
     </dict>
   </plist>