Reputation: 5129
How do you get the parent process id of a process that is not the current process in Ruby?
I've checked Ruby's Process module, but it only seems to provide a means to access the PPID of the current process.
I also checked google for anything on the subject, but the first two pages seemed to only contain links regarding how to use the aforementioned Process module.
I was hoping to do this without having to rely too much on the underlying OS, but whatever works.
Upvotes: 4
Views: 2586
Reputation: 455
Process.ppid
returns the parent process id.
http://ruby-doc.org/core-2.4.1/Process.html#method-c-ppid
Upvotes: 5
Reputation: 54734
You can just remember it in a variable:
parent_pid = Process.pid
Process.fork do
child_pid = Process.pid
puts parent_pid, child_pid
# do stuff
exit
end
Process.wait
# 94791
# 94798
alternatively, if you need the information on the level of the parent process:
parent_pid = Process.pid
child_pid = Process.fork do
# do stuff
exit
end
Process.wait
puts parent_pid, child_pid
# 6361
# 6362
Upvotes: 2