[uClinux-dev] Daemonize a process

Roope Anttinen roope.anttinen at indagon.com
Thu Mar 13 04:18:32 EST 2003


Try something like this. Fairly standard stuff but I haven't tried this
is uClinux so it might need some modifications.

int daemonize()
{
    int fd;
    pid_t pid;

    pid = fork();

    switch (pid) {
    case -1:
        return -1;
    case 0:
        break;
    default:
        _exit(pid);
    }

    if (setsid() == -1)
        return -1;
    chdir("/");
    fd = open(_PATH_DEVNULL, O_RDWR, 0);
    if (fd != -1) {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
        if (fd > 2)
            close(fd);
    }
    return 0;
}

/Roope

On Thu, 2003-03-13 at 10:01, Markus Pietrek wrote:
> Hi
> 
> > A simple way to daemonize a process:
> > 	my_process&
> 
> This is not actually a daemon. What happens, if you close the terminal or 
> console? As my_process belongs to the same process group as the shell 
> connected to the terminal, my_process is killed, too. Even close(0-2) doesn't 
> help. This doesn't happen to a daemon.
> 
> > This runs the process in the background.
> > Another way is to start the process is to use /etc/inittab.
> 
> But what if you want to start the daemon only in case some drivers have been 
> loaded? Or some jumpers/switches have been set?
> 
> Bye,
> 
> -- 
> Markus Pietrek
> FS Forth-Systeme GmbH
> Phone: +49 (7667) 908 145,  FAX +49 (7667) 908 221
> 
> 
> _______________________________________________
> uClinux-dev mailing list
> uClinux-dev at uclinux.org
> http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
> This message was resent by uclinux-dev at uclinux.org





More information about the uClinux-dev mailing list