This is the code I've used in a CGI to "background" the program so that does not happen. Call this function first thing in your CGI program main().
Code:
int daemon_init(void) {
pid_t pid;
int j, fd;
pid=fork(); //child=0, parent=pid of child or -1 on error
if (pid<0) return 0; //-1 failed
if (pid>0) exit(0); //pid of child, parent terminates
//pid is 0, child continues
//setsid(); //become session leader
chdir("/"); //change working directory
umask(0); //clear our file mode creation mask
for (j=0; j<3; j++) {
close(j);
fd=open("/dev/null",j ? O_WRONLY : O_RDONLY);
if (fd!=j) break;
}
return 3==j;
} //daemon_init()