Module: Agent::Lock::ProcessInfo
- Defined in:
- lib/agent/lock/process_info.rb,
lib/agent/lock/process_info/legacy_start.rb
Overview
The three questions this gem asks the operating system about a process: what it is called, who its parent is, and when it started.
ps rather than /proc, because this has to work the same on macOS and on
Linux, and because none of it is on a hot path. Every answer is nil when
the process is gone, which callers read as "no longer running".
Defined Under Namespace
Modules: LegacyStart
Constant Summary collapse
- FIXED_CLOCK =
The environment
psreads a start time under. Left to the caller's own,psprints it in that time zone and locale, so a command run under another TZ or LC_TIME read a different start time for the same process, took a live holder for dead, and reaped its lock. { "LC_ALL" => "C", "TZ" => "UTC" }.freeze
- LSTART =
ps -o lstart=under FIXED_CLOCK, e.g. "Sat Nov 28 21:57:53 2026". /\A[A-Z][a-z]{2} (?<month>[A-Z][a-z]{2}) +(?<day>\d+) (?<clock>\d\d:\d\d:\d\d) (?<year>\d{4})\z/
Class Method Summary collapse
-
.alive?(pid, started_at: nil, started: nil) ⇒ Boolean
Whether that exact process is still running.
-
.command(pid) ⇒ String?
The executable's name, without its path.
- .parent_of(pid) ⇒ Integer?
-
.ps(pid, field, env: {}) ⇒ String?
The field, or nil for a process that is gone.
-
.started_at(pid) ⇒ String?
When the process started, which is what tells a live process from a different one that inherited its recycled pid.
Class Method Details
.alive?(pid, started_at: nil, started: nil) ⇒ Boolean
Returns whether that exact process is still running.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/agent/lock/process_info.rb', line 61 def alive?(pid, started_at: nil, started: nil) return false if pid.nil? now = ProcessInfo.started_at(pid) return false if now.nil? return now == started_at unless started_at.to_s.empty? return true if started.to_s.empty? LegacyStart.shows?(started, Time.iso8601(now)) rescue ArgumentError # `ps` printed no date to compare with, so the pid is all there is, # as it is for a record that names no start time. true end |
.command(pid) ⇒ String?
Returns the executable's name, without its path.
29 30 31 32 |
# File 'lib/agent/lock/process_info.rb', line 29 def command(pid) value = ps(pid, "comm") value && File.basename(value) end |
.parent_of(pid) ⇒ Integer?
36 37 38 39 |
# File 'lib/agent/lock/process_info.rb', line 36 def parent_of(pid) value = ps(pid, "ppid") value && Integer(value, exception: false) end |
.ps(pid, field, env: {}) ⇒ String?
Returns the field, or nil for a process that is gone.
80 81 82 83 84 85 86 87 88 |
# File 'lib/agent/lock/process_info.rb', line 80 def ps(pid, field, env: {}) return nil if pid.nil? out = IO.popen(env, ["ps", "-p", pid.to_s, "-o", "#{field}="], err: File::NULL, &:read) out = out.to_s.strip out.empty? ? nil : out rescue SystemCallError nil end |
.started_at(pid) ⇒ String?
When the process started, which is what tells a live process from a different one that inherited its recycled pid. The same string for one process whatever the caller's time zone and locale.
48 49 50 51 52 53 54 |
# File 'lib/agent/lock/process_info.rb', line 48 def started_at(pid) text = ps(pid, "lstart", env: FIXED_CLOCK) date = LSTART.match(text.to_s) return text if date.nil? Time.utc(date[:year], date[:month], date[:day], *date[:clock].split(":")).iso8601 end |