Class: ProcessWatch

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ProcessWatch

# # example : run test_daemon - test_daemon runs forever # # don’t forget to kill the test_daemon pid when you are done playing. # $ hamster –watch ‘test_daemon ID=1’ –command “test_daemon ID=1”



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/process_watch.rb', line 16

def initialize(options)
  self.command          = options[:command]              || nil
  self.callback         = options[:callback]             || 
                           lambda { p 'default callback does nothing'}
  self.callback_message = (options[:callback_message]    || 
                           "Callback Triggered").strip
  self.watch_string     = (options[:watch]               || 
                           "this poem is a pomme").strip
  self.delay            = options[:delay]                || 60
  self.num_cycles       = options[:num_cycles]           || 3
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



2
3
4
# File 'lib/process_watch.rb', line 2

def callback
  @callback
end

#callback_messageObject

Returns the value of attribute callback_message.



2
3
4
# File 'lib/process_watch.rb', line 2

def callback_message
  @callback_message
end

#commandObject

Returns the value of attribute command.



2
3
4
# File 'lib/process_watch.rb', line 2

def command
  @command
end

#delayObject

Returns the value of attribute delay.



2
3
4
# File 'lib/process_watch.rb', line 2

def delay
  @delay
end

#num_cyclesObject

Returns the value of attribute num_cycles.



2
3
4
# File 'lib/process_watch.rb', line 2

def num_cycles
  @num_cycles
end

#watch_stringObject

Returns the value of attribute watch_string.



2
3
4
# File 'lib/process_watch.rb', line 2

def watch_string
  @watch_string
end

Class Method Details

.find(watch_string, ps_args = "") ⇒ Object

-xf => my proccesses => $1 is parent pid of watch_string match -x => my processes => $1 is pid of of watch_string match



33
34
35
36
# File 'lib/process_watch.rb', line 33

def find(watch_string,ps_args = "")
  cmd = %%ps -x#{ps_args} |grep '#{watch_string}' |grep -v grep | grep -v 'watch #{watch_string}' | awk '{print $1}'%
  pids = %x{#{cmd}}.split()
end

Instance Method Details

#cycleObject



40
41
42
43
44
45
# File 'lib/process_watch.rb', line 40

def cycle
  self.num_cycles.times do 
    _check
    sleep self.delay
  end
end