开发者

Ruby prog won't run as LaunchDaemon

I have a ruby program, pretty simple, just does DNS lookup on a bunch of hosts. The program runs fine from the terminal (Mac OS X 10.6.5), Ruby 1.9.2 using RVM.

I would like to run this on a schedule and would like to load a LaunchDaemon to do so. The program does not run as a LauanchDaemon, and the output files are created, but empty, so I am unable to see the error(s). I suspect it has something to do with the includes.

Here is the plist (it loads fine and attempts to run as evidenced by the creation of both output files):

<?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.jsw.ping</string>
    <key>RunAtLoad</key>
    <true/>
    <key>Debug</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/var/jsw/logs/ping.txt</string>
    <key>EnvironmentVariables</key>
        <dict>
            <key>PATH</key>
                <string>/Users/scott/.rvm/gems/ruby-1.9.2-p0/bin:/Users/scott/.rvm/gems/ruby-1.9.2-p0@global/bin:/Users/scott/.rvm/rubies/ruby-1.9.2-p0/bin:/Users/scott/.rvm/gems/ruby-1.9.2-p0@rails3/bin:/Users/scott/.rvm/gems/ruby-1.9.2-p0@global/bin:/Users/scott/.rvm/rubies/ruby-1.9.2-p0/bin:/Users/scott/.rvm/bin:/usr/local/maven/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin</string>
        </dict>
    <key>StandardErrorPath</key>
    <string>/var/jsw/logs/ping.err.txt</string>
    <key>ProgramArguments</key>
    <array>
        <string>ping.rb</string>
    </array>
    <key>StartInterval</key>
      <integer>1800</integer>
    <key>WorkingDirectory</key>
        <string>/Library/WebServer/vHosts/scripts/ping/</string>
   <key>UserName</key>
       <string>scott</string>
  </dict>
</plist>

The Ruby program is:

#!/Users/scott/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
require 'rubygems'  
require 'active_record'
require 'yaml'  
require 'dnsruby'
include Dnsruby


dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)

# flush cache
rslt = `dscacheutil -flushcache`

Dnsruby::DNS.open 
rslvr = Dnsruby::DNS.new

now = DateTime.now.to_s
mysqlDT = now.gsub("T", " ")
puts mysqlDT


class Host < ActiveRecord::Base  开发者_开发知识库
end  
class Ip < ActiveRecord::Base  
end  

Host.find_each do |svr|
  ip = rslvr.getaddress(svr.host).to_s
  svr.lastIP = ip
  svr.save
  Ip.create(:ip => ip, :host => svr.host, :DT => mysqlDT, :hostID => svr.id)
end


The root problem was that the LaunchDaemon job was being run from a shell that did not set up RVM properly and thus the included classes could not be located. Since I don't have any legacy Ruby programs on my system, I removed RVM and installed Ruby 1.9.2 in /usr/local/bin on my system and then re-installed gems.

The second problem (no logging) was due to a permissions error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜