Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在IO中找到ruby sendmail after模式_Ruby_Io_Logging_Sendmail - Fatal编程技术网

在IO中找到ruby sendmail after模式

在IO中找到ruby sendmail after模式,ruby,io,logging,sendmail,Ruby,Io,Logging,Sendmail,我肯定我错过了什么。基本上,我想监控日志IO,如果记录了致命错误,则发送一封包含错误的电子邮件 #!/usr/bin/ruby -w require 'rubygems' def mailer(line) date = `date +%D-%T` f = File.open("/root/error.mail", "w") f.puts("Subject: Fatal Error on SERVER #{date}\n\n#{line}") f.close system

我肯定我错过了什么。基本上,我想监控日志IO,如果记录了致命错误,则发送一封包含错误的电子邮件

#!/usr/bin/ruby -w

require 'rubygems'

def mailer(line)

  date = `date +%D-%T`
  f = File.open("/root/error.mail", "w")
  f.puts("Subject: Fatal Error on SERVER #{date}\n\n#{line}")
  f.close
  system("sendmail guy@foo.com.com < /root/error.mail")
end

def fatal_check(file, pattern)

  f = File.open(file, "r")
  f.seek(0,IO::SEEK_END)
  while true do
    select([f])
    line = f.gets
    mailer("#{line}") if line=~pattern
    #system("./mailer.rb #{line}") if line=~pattern
  end
end

fatal_check("/root/test.log", /FATAL ERROR/)
#/usr/bin/ruby-w
需要“rubygems”
def邮件器(线路)
日期=`date+%D-%T`
f=文件.open(“/root/error.mail”,“w”)
f、 puts(“主题:服务器上的致命错误#{date}\n\n#{line}”)
f、 接近
系统(“发送邮件guy@foo.com.com
这个怎么样。你需要一些宝石:

gem install file-tail
gem install pony
然后是您的脚本:

require 'rubygems'
require 'pony'
require 'file/tail'

def fatal_check(file, pattern)
  File::Tail::Logfile.open(file, :backward => 0) do |log|
    log.tail do |line| 
      date = `date +%D-%T`
      Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => "There was a nasty error on #{date}", :body => line)
    end
  end
end

fatal_check(File.dirname(__FILE__) + "/test.log", /FATAL/)

为什么你的代码会失败?编辑上面的问题并解释问题。