Hi there,
I'm relatively new to ruby and I'm trying to put together this script to process cron jobs on a server. It's almost working, except I can't figure out what exactly is wrong and how to fix it. Can anyone point me in the right direction?
Here's what I have so far:
require 'rubygems'
require 'crontab-parser'
start_time = Time.parse(ARGV[0])
end_time = Time.parse(ARGV[1])
File.open('/root/flagged_cron_jobs.log', 'w+') do |f2|
crontab_data = File.readlines(ARGV[2]).select {|line| line =~ /^[0-9*]/}
cron = CrontabParser.new(crontab_data.join("\n"))
(start_time..end_time).each do |timestamp|
next unless timestamp.sec.to_i == 0
cron.each do |cron_entry|
Dir["/var/spool/cron/*"].each do |file|
username = file.split("/").last
crontab = CrontabParser.new(File.read(file))
puts "User: #{username}"
if cron_entry.should_run?(timestamp)
f2.puts "#{timestamp}\t#{cron_entry.cmd}"
end
end
end
end
end
Here's the error:
cron_parse.rb:9:in `readlines': can't convert nil into String (TypeError)
from cron_parse.rb:9
from cron_parse.rb:7:in `open'
from cron_parse.rb:7
Command being executed is like so:
ruby cron_parse.rb "Thurs Feb 7 01:00 UTC 2013" "Thurs Feb 7 01:14 UTC 2013"
I suspect it's because of the addition of
crontab = CrontabParser.new(File.read(file))
in relation to
cron = CrontabParser.new(crontab_data.join("\n"))
but I'm not really sure of how to deal with this.