0

It's possible I can't use launchd for this, I just couldn't find anything saying explicitly NO. So, here's the question...

I'd like to run my Watir scripts 10 minutes past the hour, every hour. This script launches Firefox, performs tests, and logs results to standard output. I setup my plist file like this:

<?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>client_checkout</string>
  <key>Program</key>
  <string>/Users/chris/svn/qa/watir/tests/client.sh</string>
   <key>StartCalendarInterval</key>
    <dict>
        <key>Minute</key>
        <integer>52</integer>
    </dict>
</dict>
</plist>

Inside that shell script I have:

echo 'hello' >> /Users/chris/results.txt
ruby /Users/chris/svn/qa/watir/tests/client_checkout.rb

The echo is there for debug purposes.

When I place the plist file in /Library/LaunchDaemons the script runs and I see 'Hello' written to the text file. Then, it fails when it hits the Ruby part. When I look at system.log I see:

com.apple.launchd.peruser.502[118] (client_checkout[25285]): Exited with code: 1

I tried switching the plist file over to /Library/LaunchAgents but it's the same exact results.

Also, I don't think it's a file path problem. When I provide the full file path, as I'm doing here, these scripts run fine from any directory.

Can I not use launchd this way? Am I "doing it wrong"? What's going on? Thank you!

jonsie_araki
  • 221
  • 3
  • 8

2 Answers2

1

I can think of two things that might be the problem there. The enviroment may not be getting setup properly. I would try to run it from sh (or bash), and perhaps check to make sure things like your $PATH are configured how you expect.

<?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>client_checkout</string>

  <key>ProgramArguments</key>
    <array>
      <string>/bin/sh</string> <!-- New bit here -->
      <string>/Users/chris/svn/qa/watir/tests/client.sh</string>
    </array>

   <key>StartCalendarInterval</key>
    <dict>
        <key>Minute</key>
        <integer>52</integer>
    </dict>
</dict>
</plist>

If you want to check on the environment you can change your script to be something like this:

env > /Users/chris/Desktop/launchdenv.txt

The other thing that might be causing issues would be running firefox w/o a gui environment. You might have better luck moving your launchd script to ~/Library/LaunchAgents/.

csexton
  • 24,061
  • 15
  • 54
  • 57
0

It was failing because it couldn't find my Gems, which are in /Users/chris/.rvm, not /usr/. Found what I needed to add to my script on RVMs website, and now I'm running! For anyone else with the same problem, the code you need from RVM can be found @ https://rvm.io/workflow/scripting/

Specifically, add this to the start of your shell script:

    # Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then

  # First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"

elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then

  # Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"

else

  printf "ERROR: An RVM installation was not found.\n"

fi
jonsie_araki
  • 221
  • 3
  • 8