From: Ben Giddings Date: 2005-03-31T09:13:36+09:00 Subject: Re: Getting process id of started process Joe Van Dyk wrote: > I've attatched code and test code at > http://rafb.net/paste/results/2Rj4rb87.html , if someone wants to take > a look. Hopefully it's clear about what I want to do. > > > On Wed, 30 Mar 2005 14:57:51 -0800, Joe Van Dyk wrote: > >>Say I want to start a long running shell process (and monitor that process). >> >> def start_process >> @process_id = fork do >> Kernel.system "cat /dev/zero > /dev/null" >> end >> end >> >>However, @process_id doesn't contain the pid of the cat process. How >>can I capture the pid of that process? I think what you need is "$?", which is "The status of the last executed child process." You might want to look at the docs here: http://www.ruby-doc.org/core/classes/Kernel.html#M001773 It says that in block form, fork returns zero, but it might set $?, I'm not sure. In any case, you might need to use the non-block form of fork to do what you want. That way you'll either get the process ID or nil: def start_process @process_id = fork if @process_id # We're in the parent else system("cat /dev/zero > /dev/null") Process.exit!(0) end @process_id end Anyhow, the Process documentation seems to be pretty good: http://www.ruby-doc.org/core/classes/Process.html Ben