Amanda
Amanda

Reputation: 454

Running postgres query in launchd

I'm trying to run a postgres query at regular intervals on OSX Sierra, and I found the recommendation that I should use launchd. I created a plist file that calls a simple shell script containing a postgres query, copied the plist file into the LaunchAgents folder, and loaded it. However, it doesn't seem to be working.

If the shell script I'm running is something simple like:

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

it works just fine - every 30 seconds, it refreshes that file with the word "Hello". However, if it's a postgres query like the following, I don't get any output.

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

Both of these commands work just fine when I run them from the command line, but the postgres query doesn't work when called by the LaunchAgent. I don't have much knowledge at all about either postgres or launchd, so I'd appreciate any insight into what I'm doing wrong.

In case it's relevant, here's my launchd file:

<?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>

(where test.sh simply contains the command I'm trying to run)

Edit: If no one is familiar with this particular issue, is there any easy way to debug launchd? If either launchd or postgres is reporting an error, is there a file I could check for an error message/code?

Upvotes: 0

Views: 155

Answers (1)

Tatu Lahtela
Tatu Lahtela

Reputation: 4554

Use the Launchd configuration to set the STDOUT location, not pipe (> ..txt) in the shell script.

    <!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>

Upvotes: 1

Related Questions