Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a process that I am starting from a terminal which may or may not remain open. I've been starting the perl script with "&" (myscript.pl &), but the script instantly dies when the controlling terminal is closed.

How does one prevent this?

Replies are listed 'Best First'.
Re: Detach from terminal
by halley (Prior) on Aug 26, 2003 at 15:48 UTC
    Another method is to simply ignore the HUP signals which get raised by a dying controlling terminal.
    $ nohup myscript.pl &

    --
    [ e d @ h a l l e y . c c ]

Re: Detach from terminal
by Fletch (Bishop) on Aug 26, 2003 at 15:35 UTC

    Search perldoc perlipc for "Complete Dissociation of Child from Parent". I think Net::Daemon also will do this for you.

Re: Detach from terminal
by Mr_Person (Hermit) on Aug 26, 2003 at 16:15 UTC
    I'd recommend taking a look at Proc::Daemon. It's fairly simple to use and makes sure that you are completely detached from any terminal.
Re: Detach from terminal
by VSarkiss (Monsignor) on Aug 26, 2003 at 15:49 UTC

    Not clear from your description, but it sounds like a shell issue, not a Perl issue. Try starting the program with, e.g., nohup myscript.pl &. You can read about nohup in the documentation for your particular shell.

Re: Detach from terminal
by Anonymous Monk on Aug 26, 2003 at 15:40 UTC
    Nevermind. I decided to try a fork and that seems to work well.

    my $pid = fork(); exit(1) if $pid < 0; exit(0) if $pid > 0;