############################################################################### # Class: CronJobMgr # This class is a child of the array class that provides methods for loading # and saving cron jobs to a text file of the user's choosing. ############################################################################### class CronJobMgr < Array ####################################################################### # loadCronJobs # Open the file containing the cron jobs and read each into the list # of cron jobs. ####################################################################### def loadCronJobs(filename) # Create the file if it doesn't already exist File.open(filename, File::CREAT|File::RDONLY) do |file| file.each_line { |line| # Get each of the job's attributes from the # current line in the cron jobs file jobAttributes = line.split() # Add everything after the weekday to the # command string 6.upto(jobAttributes.length - 1) { |i| jobAttributes[5] = jobAttributes[5] + " " + jobAttributes[i] } # Create a new cron job, populate it, and push # it into the array job = CronJob.new(jobAttributes[0], jobAttributes[1], jobAttributes[2], jobAttributes[3], jobAttributes[4], jobAttributes[5]) self.push(job) } end end ####################################################################### # saveCronJobs # Save all of the cron jobs in the list to a text file. ####################################################################### def saveCronJobs(filename) # Open the file for writing (overwrite previous information) File.open(filename, "w") do |file| self.each { |job| file.puts(job) } end # Call the crontabs command and add the jobs to the schedule system("crontab", filename) end ####################################################################### # to_s ####################################################################### def to_s self.each { |job| puts job } end end ############################################################################### # Class: CronJob ############################################################################### class CronJob attr_reader :minute, :hour, :day, :month, :weekday, :command attr_writer :minute, :hour, :day, :month, :weekday, :command ####################################################################### # commandName # This method parses the command name out of the command string ####################################################################### def commandName dirs = @command.split('/') dirs[dirs.length - 1] end ####################################################################### # initialize # This method initializes the new job to the values passed in. ####################################################################### def initialize(minute="*", hour="*", day="*", month="*", weekday="*", command="*") @minute = minute @hour = hour @day = day @month = month @weekday = weekday @command = command end ####################################################################### # to_s ####################################################################### def to_s "#{@minute}\t\t#{@hour}\t\t#{@day}\t\t#{@month}\t\t" + "#{@weekday}\t\t#{commandName}" end end