Search code examples
rubyautomationwatirwatir-webdriverlaunchd

Watir scripts via launchd


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!


Solution

  • 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/.