in reply to Re: how to make a demon in perl?
in thread how to make a demon in perl?

I'd modify this code to fork first, then close all the filehandles - especially STDERR: what's the point of calling "die" if your STDERR is closed?

-- Dan

Replies are listed 'Best First'.
Re: Re: Re: how to make a demon in perl?
by blm (Hermit) on Oct 01, 2002 at 13:14 UTC

    Silly me! Yes, calling die when STDERR is redirected to /dev/null seems pointless except to stop the script. For this exit would suffice.

    That said, I believe it is done before the fork is perform because the child inherits the file descriptors like STDIN and STDOUT.(1)

    Nevertheless, I made the modifications you suggest and the daemon is still detached from the terminal such that it keeps running when I disconnect my terminal. Maybe someone can explain to me what is happening here?

    --blm--

      Right - the child inherits the file descriptors of the parent, which is why you need to close them. So you can either close them before the fork, which will close them both for the parent and the child, or after the fork, which will close them for the child, but leave them open for the parent.

      Perhaps I don't understand your question? Why wouldn't the child in this case continue running after the disconnect?

      -- Dan

        I can see what is happening I am just having trouble understanding why it works

        I figured the order was important. I thought if the child inherited normal STDIN etc then redirected them it would be the same as running a normal script and trying to detach by redirecting STDIN etc (see 1). The child wouldn't be detached and would die on SIGHUP or whatever.

        I might just have to accept that I see it but don't fully understand it. Thanks for your posts though!


        (1) I have tried the daemon script without fork and it doesn't work since just doing the redirecting of file descriptors of a process doesn't seem to detach.

        --blm--