【问题标题】:Running postgres query in launchd在 launchd 中运行 postgres 查询
【发布时间】:2018-03-08 12:34:22
【问题描述】:

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

如果我正在运行的 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

当我从命令行运行这两个命令时,它们都可以正常工作,但是当被 LaunchAgent 调用时,postgres 查询不起作用。我对 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 报告错误,是否有可以检查错误消息/代码的文件?

【问题讨论】:

  • 尝试将 stderr 重定向到 psql ... &gt; ... 2&gt; error.txt 的文件,这可能会给你一个线索

标签: macos postgresql psql launchd


【解决方案1】:

使用 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>

【讨论】:

  • 谢谢!我至少可以明白为什么它现在不起作用了!事实证明它无法找到 psql,即使它在我的路径中。 (psql 位于 /usr/local/bin 中,这是 $PATH 中的第一个条目)如果我包含 psql 的完整路径,它现在可以工作,但是 launchd 会在其他地方检查 PATH 吗?
  • 我认为默认情况下几乎没有传递任何内容。您可以使用 EnvironmentVariables 将 PATH 设置为您想要的。
猜你喜欢
  • 2018-01-23
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-02
  • 2015-03-29
  • 2019-03-17
相关资源
最近更新 更多