Class: Crontab::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/crontab/entry.rb

Overview

A class which represents a job line in crontab(5).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schedule, command, uid = nil) ⇒ Entry

Creates a crontab(5) entry.

  • schedule A Crontab::Schedule instance.

  • command

  • uid

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/crontab/entry.rb', line 9

def initialize(schedule, command, uid=nil)
  raise ArgumentError, 'invalid schedule' unless schedule.is_a? Schedule
  raise ArgumentError, 'invalid command' unless command.is_a? String

  @schedule = schedule.freeze
  @command = command.freeze
  @uid =
    case uid
    when String
      Etc.getpwnam(uid).uid
    when Integer
      uid
    when nil
      Process.uid
    else
      raise ArgumentError, 'invalid uid'
    end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



28
29
30
# File 'lib/crontab/entry.rb', line 28

def command
  @command
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



28
29
30
# File 'lib/crontab/entry.rb', line 28

def schedule
  @schedule
end

#uidObject (readonly)

Returns the value of attribute uid.



28
29
30
# File 'lib/crontab/entry.rb', line 28

def uid
  @uid
end

Class Method Details

.parse(line, options = {}) ⇒ Object

Parses a string line in crontab(5) job format.

  • options[:system] when true system wide crontab is assumed and @uid is extracted from line.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/crontab/entry.rb', line 34

def parse(line, options={})
  options = { :system => false }.merge(options)
  line = line.strip
  number_of_fields = 1
  number_of_fields += line.start_with?('@') ? 1 : 5
  number_of_fields += 1 if options[:system]
  words = line.split(/\s+/, number_of_fields)
  command = words.pop
  uid = options[:system] ? words.pop : Process.uid
  spec = words.join(' ')
  schedule = Crontab::Schedule.new(spec)
  new(schedule, command, uid)
end