The following is a simple script that is designed to run an application and, if the application produces any output, e-mail that application’s output to a given address. I use this for several of my custom scripts used for updates and backups. I have an e-mail mailbox setup just for logging purposes. To use this script, simply replace bebop.turtles.sumdami.net with the address of your local SMTP server.
#!/usr/bin/env ruby
require 'net/smtp'
def mailOutput(command,from,to,subject)
msghead = "From: #{from}\nTo: #{to}\nSubject: #{subject}\n\n"
msgstr = ""
IO.popen(command+' 2>&1') { |f| msgstr += f.read }
if not msgstr.strip.empty? then
smtp = Net::SMTP.start("bebop.turtles.sumdami.net",25)
smtp.send_message msghead+msgstr,from,to
smtp.finish
end
end
if ARGV.length != 4 then
puts 'Usage: email_log.rb <command> <from> <to> <subject>'
exit 1
else
mailOutput ARGV[0],ARGV[1],ARGV[2],ARGV[3]
exit 0
end