Re: Understanding forkby RMGir (Prior)
|on Nov 08, 2002 at 16:44 UTC||Need Help??|
Seems fine to me.
Here's what's happening. When fork is called, it either fails or succeeds. If it fails, it returns undef, so you log an error and DieNice.
If it succeeds, it returns _twice_. Well, in reality, it spawns a separate process with its own copy of all your variables, file descriptors, etc, and both process run in parallel.
In the parent process, fork() returns the process ID of the new child. So in that process, you redirect the user, and exit. (At least, I THINK you should exit, but you're falling out of that else clause in your sample, and doing the while twice...).
The child process gets 0 back from fork. That process closes STDIN, STDOUT, and STDERR to detach itself from the parent process, and continues on to do the normal processing. Closing those 3 filehandles is not enough to completely detach in all situations. However, since you're being run by the web server, you don't have to worry about things like terminals. If you have any other files open before the fork, you should probably close them, as well.
You should consider doing an open of STDERR to a log file after the close(STDERR), in case your script dies/warns...
Edit: Was missing an important NOT in the "enough to completely detach" sentence...