Class: HawatelPS::Linux::ProcControl

Inherits:
Object
  • Object
show all
Defined in:
lib/hawatel_ps/linux/proc_control.rb

Direct Known Subclasses

ProcInfo

Instance Method Summary collapse

Instance Method Details

#resumeString

Resume suspended process (equal to Kill -SIGCONT <pid> command)

Examples:

p = HawatelPS.search_by_name('top')
p.resume

Returns:

  • (String)
    • current process status (running, stopped ..)



46
47
48
49
50
51
52
53
54
# File 'lib/hawatel_ps/linux/proc_control.rb', line 46

def resume
  process_status = status
  if process_status  == 'stopped'
    return status if Process.kill('CONT', @proc_attrs[:pid].to_i)
  end
  process_status
rescue Errno::EPERM
  return 'non-privilaged operation'
end

#statusString

Check current process status

Examples:

p = HawatelPS.search_by_name('top')
p.status

Returns:

  • (String)
    • current process status (running, stopped ..)



11
12
13
14
15
16
17
18
19
20
# File 'lib/hawatel_ps/linux/proc_control.rb', line 11

def status
  sleep(0.2)
  status_file = "/proc/#{@proc_attrs[:pid]}/status"
  if File.readable?(status_file)
    File.foreach(status_file).each do |attr|
      return attr.split(' ')[2].chop[1..-1] if attr =~ /State:/
    end
  end
  'terminated'
end

#suspendString

Suspend process without loss data (equal to Kill -SIGSTOP <pid> command)

Examples:

p = HawatelPS.search_by_name('top')
p.suspend

Returns:

  • (String)
    • current process status (running, stopped ..)



29
30
31
32
33
34
35
36
37
# File 'lib/hawatel_ps/linux/proc_control.rb', line 29

def suspend
  process_status = status
  if process_status != 'terminated' && process_status != 'stopped'
    return status if Process.kill('STOP', @proc_attrs[:pid].to_i)
  end
  process_status
rescue Errno::EPERM
  return 'non-privilaged operation'
end

#terminateString

Kill process (equal to Kill -SIGKILL <pid> command)

Examples:

p = HawatelPS.search_by_name('top')
p.kill

Returns:

  • (String)
    • current process status (running, stopped ..)



63
64
65
66
67
68
69
70
71
# File 'lib/hawatel_ps/linux/proc_control.rb', line 63

def terminate
  process_status = status
  if process_status != 'terminated'
    return status if Process.kill('KILL', @proc_attrs[:pid])
  end
  process_status
rescue Errno::EPERM
  return 'non-privilaged operation'
end