From: tamouse mailing lists Date: 2013-02-10T08:51:09+09:00 Subject: Re: New to ruby - need assistance with rewriting a script On Fri, Feb 8, 2013 at 9:00 AM, Ronald Craft wrote: > Hello, > > I'm working on learning ruby and trying to figure out how I would take > an existing example and rewrite it in a way that suits my own usage. > What I'm looking for here isn't for someone to just give me a block of > fixed code and be like, "Oh, here you go!" since I won't actually learn > anything from that. > > So, here's the script: > > #### > require 'rubygems' > require 'crontab-parser' > > start_time = Time.parse(ARGV[0]) > end_time = Time.parse(ARGV[1]) > > them > cron_data = File.readlines(ARGV[2]).select {|line| line =~ /^[0-9*]/} > cron = CrontabParser.new(cron_data.join("\n")) > (start_time..end_time).each do |timestamp| > next unless timestamp.sec.to_i == 0 > cron.each do |cron_entry| > if cron_entry.should_run?(timestamp) > puts "#{timestamp}\t#{cron_entry.cmd}" > end > end > end > #### > > Again, not my code. What I want to do is like so: > > 1. Run this script without requiring any CLI arguments. I know the time > interval I want to search for (between 1 and 14 minutes) but I don't > want to have to type out the range of times when I execute the script. > I've seen the chronic gem and believe this is the answer I'm looking > for, but not certain of how I'd integrate that in here. > > 2. Scan all of the files in a directory with the exception of two (of > which I'd need to set an .excludes). Something like this (my code): > > #### > Dir["/var/spool/cron/*"].each do |file| > username = file.split("/").last > crontab = CrontabParser.new(File.read(file)) > > crontab.each do |cron| > puts "User: #{username}" > # Do whatever > end > end > #### > > This way I can get the output of: > > User1 > cron1 > cron2 > > User2 > Cron1 > Cron2 > > And so on. > > Can anyone offer some sort of assistance in helping me get this to where > I want it? I'm happy with any kind of relevant documentation for this > task (there's so much unrelated documentation that I end up > lost/confused), examples that are similar in what I'm trying to do, or > partial > answers that will point me in the right direction. > > I know this may seem a bit unorthodox, but it's how I learn - I can't > just read a book on ruby and understand how to do everything (I've read > Chis Pine's book and it has helped a little, but not enough for me to be > able to do something like this on my own). > > Thanks in advance! > > -- > Posted via http://www.ruby-forum.com/. > You might want to look at TImeCop in association with Chronic.