You need at least 2 forks to be certain that the final process is completely independent from any "controlling terminal".

See http://linux.about.com/library/cmd/blcmdl2_setsid.htm for a partial description of the underlying mechanism.

In Ruby, this is available from Process.setsid

http://www.ruby-doc.org/core-1.9.3/Process.html#method-c-setsid

From Ruby, you probably want to use Process.daemon, which uses a higher-level call that is made up of standardized (by POSIX) system calls though daemon itself is not a standard system call.

http://www.ruby-doc.org/core-1.9.3/Process.html#method-c-daemon

-Rob

P.S. If you want more details, you can read up here:

http://stackoverflow.com/questions/2613104/why-fork-before-setsid
http://stackoverflow.com/questions/7645378/whats-the-difference-between-calling-daemon-and-calling-fork-setsid-for
http://blog.samat.org/2005/06/02/the_semantics_of_a_fork_followed_by_setsid_and_another_fork
	http://code.activestate.com/recipes/278731/
	 [Warning: Python code ;-) but the comments go into great detail
	 on why the underlying system calls are being made

On Feb 2, 2012, at 2:29 AM, Victor Blaga wrote:

> Hi,
> 
> I need to start a (sub)process from inside a ruby script that will continue
> to run after the ruby script itself has finished.
> The way I'm doing stuff right now is by using Process.spawn - this works,
> however the ppid of the child process is assigned to the script that was
> used to start the process, and thus after the script finishes the spawned
> (child) processes gets closed as well.
> 
> My question is what would be the best way to start such a (sub)process and
> assign it a parent in such a way that it keeps running even after the
> current script has ended? As a side-note, I also need to be able to capture
> the stderr and assign it to a specific file (which works using
> Process.spawn), but I'm not sure about the other methods.
> I have to mention I'm using a linux machine but I'm unfortunately not very
> familiar with the way linux process exactly work...
> 
> Thank you,
> Victor