在launchd中运行postgres查询

在launchd中运行postgres查询

问题描述:

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

如果我运行shell脚本是一些简单的像:

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

它工作得很好 - 每30秒,它刷新该文件以单词“你好”。但是,如果它是像以下这样的postgres查询,我不会得到任何输出。

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

这些命令都工作得很好,当我在命令行中运行它们,但当由​​LaunchAgent称为Postgres的查询不起作用。我对postgres或launchd都没有太多的了解,所以我希望能够深入了解我做错了什么。

如果它是相关的,这里是我的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报告错误,是否有文件可以检查错误消息/代码?

+0

尝试重定向错误输出到一个文件,以'PSQL ...> ... 2> error.txt'它可能给你一个线索,什么是错的 – Eelke

使用Launchd配置来设置STDOUT位置,而不是shell脚本中的pipe(> ..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> 
+0

谢谢!我至少可以明白为什么它现在不工作!原来,它无法找到psql,即使它在我的路径中。 (psql位于/ usr/local/bin,这是$ PATH中的第一个条目)如果我包含psql的完整路径,它现在可以工作,但确实启动时检查PATH的其他位置? – Amanda

+0

我认为几乎没有任何东西是默认传递的。您可以使用EnvironmentVariables将PATH设置为所需的值。 –