switch(fork())
{
case -1: perror("fork");
case 0: break;
default: exit(0);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
signal(SIGHUP, SIG_IGN);
setsid();
More or less how a server would make itself a daemon process.
Create a new process, let the parent die so the user gets his or her
prompt back. Then close standard input and output to detach it from the
terminal. Use signal function to make the process ignore hangups.
setsid makes the process divorce the shell's process group (this may
take care of hangups in and of itself, but just to be sure I do it
explicitly).
Mine then opens a log file on stdout and stderr.
--Palrich.