添加Process.daemon时,Ruby脚本未运行

时间:2012-10-25 01:27:07

标签: ruby-on-rails ruby ruby-on-rails-3 daemon

我有以下简单的脚本,它检查电子邮件帐户,如果有新邮件,它会转发电子邮件并发送短信。当脚本在没有Process.daemon的情况下运行时,会发生这种情况。添加后,并在电子邮件帐户收到电子邮件,没有任何反应(没有任何转发,也没有发送短信),并且控制台中没有错误消息。有什么建议吗?

#!/usr/bin/env ruby
require "bundler/setup"
require "mailman"
require "twilio-ruby"

Mailman.config.pop3 = {
  :username => 'address@gmail.com',
  :password => 'password',
  :server   => 'pop.gmail.com',
  :port     => 995,
  :ssl      => true
}

Mailman.config.poll_interval = 60

Mailman::Application.run do  
  default do
    begin
      Ticket.receive_mail(message)
      MailForwarder.forwarded_email(message).deliver
      @account_sid = 'xxxxxxxxxxx'
      @auth_token = 'xxxxxxxxxx'
      @client = Twilio::REST::Client.new(@account_sid, @auth_token)
      @account = @client.account
      @sms = @account.sms.messages.create(
        :from => '+1111111111',
        :to => '+122222222',
        :body => message.subject
      )
      puts @sms
      puts "#{message.subject}"
    rescue Exception => e
      Mailman.logger.error "Exception occurred whle receiving message:\n#{message}"
      Mailman.logger.error [e, *e.backtrace].join("\n")
    end
  end
  Process.daemon
end

1 个答案:

答案 0 :(得分:1)

我相信您需要在启动mailman应用程序之前将脚本设置为守护程序。我做了一些测试,如果我在调用Mailman :: Application.run之前调用了Process.daemon,但是如果我把它放在你拥有它的地方它就没用了。

所以我把它当作:

....

Mailman.config.poll_interval = 15

Process.daemon

Mailman::Application.run do

 default do


 end

end