Ruby: "Can't convert nil into String" error


 
Thread Tools Search this Thread
Top Forums Programming Ruby: "Can't convert nil into String" error
# 1  
Old 02-07-2013
Ruby: "Can't convert nil into String" error

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:

Code:
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:

Code:
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:

Code:
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
Code:
 crontab = CrontabParser.new(File.read(file))

in relation to
Code:
cron  = CrontabParser.new(crontab_data.join("\n"))

but I'm not really sure of how to deal with this.

Last edited by jim mcnamara; 02-07-2013 at 04:53 PM..
# 2  
Old 02-07-2013
That is happening because you are running the script wrong. Your script expects 3 arguments as input -- ARGV[0], ARGV[1] and ARGV[2] but you are only providing ARGV[0] and ARGV[1].
Therefore on line 9 ARGV[2] is nil and cannot be converted to a string hence the error. You need to run it like so:

Code:
ruby cron_parse.rb "Thurs Feb 7 01:00 UTC 2013" "Thurs Feb 7 01:14 UTC 2013" Filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Convert "hex" foldername to "ascii"

So, I have a folder, containing subdirs like this: 52334d50 52365245 524b4450 524f3350 52533950 52535050 52555550 now I want to go ahead and rename all those folder: hex -> ascii (8 Replies)
Discussion started by: pasc
8 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

How to convert "Nov 9 11:35:28 2009" to "200911091135"

Using AIX 5.3 and /user/bin/ksh. Anyone have a quick way to convert the string date Nov 9 11:35:28 2009 to 200911091135 I know I could create a table of months and find the month number by searching the list, but I was hoping there was some handy little known command to do this... (15 Replies)
Discussion started by: troym72
15 Replies

9. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies

10. UNIX for Dummies Questions & Answers

Convert "text" to "packed-decimal"?

Is there a way with HP-UX Release 10.20 (but going to HP-UX 11) to convert a regular "text" file to a packed data format (such as is created by a Cobol program)? (2 Replies)
Discussion started by: HuskyJim
2 Replies
Login or Register to Ask a Question