From: Csaba Henk Date: 2005-04-02T23:34:55+09:00 Subject: Re: Getting process id of started process On 2005-03-30, 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? To ensure that the called program really runs as the child of the main process (and not a further descendent), you have to code it in a relatively low-level way; in the above case it would look like def start_process @process_id = fork do File.reopen $stdin, File.open("/dev/null","w") exec %w(cat /dev/zero) end end (In this case, 'exec "cat /dev/zero"' would do as well. Ruby's exec is a bastardized one, not just an interface to the posix exec* calls: it tries to find out by itself whether to exec the program directly or to do it via shell. In general, passing an array to exec protects from shell expansion.) I'm working on a lib which makes it easier, eg. you could do with it: def start_process po = Pope.new po.do(%w(cat /dev/zero), 1 => "/dev/null").end @process_id = po.actors[0].child end Csaba