0

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?

1
  • Try redirecting stderr to a file to psql ... > ... 2> error.txt it might give you a clue what is wrong Commented Oct 2, 2017 at 4:54

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I can at least see why it isn't working now! Turns out it wasn't able to find psql, even though it's in my path. (psql is located in /usr/local/bin, which is the first entry in $PATH) If I include the full path to psql, it works now, but does launchd check somewhere else for the PATH?
I think pretty much nothing is passed by default. You can use EnvironmentVariables to set PATH to what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.