in reply to Is it possible to background a perl script from within itself?

FWIW "starting as a daemon" is different than the "&". The latter is a form of shell job control. Others have given examples of the former.
  • Comment on Re: Is it possible to background a perl script from within itself?

Replies are listed 'Best First'.
Re: Answer: Is it possible to background a perl script from within itself?
by hbo (Monk) on Apr 18, 2003 at 06:09 UTC
    You have to setsid too, to disassociate from your parent's process group:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use POSIX qw(setsid);
    
    # Become a daemon
    my $pid=fork;
    exit if $pid; # Parent exits here
    die "Couldn't fork $!" unless defined($pid);
    die "Couldn't start new session $!" unless POSIX::setsid();
    
    # Do your daemon bit...
    
Re: Answer: Is it possible to background a perl script from within itself?
by hbo (Monk) on Apr 18, 2003 at 06:18 UTC
    OK, rereading the text at the top..

    One way to achieve the result is to resort to bash:
    #!/bin/bash
    perlprog.pl&
    
    But it seems silly just to avoid typing '&'. What exactly are you trying to achieve?
Re: Answer: Is it possible to background a perl script from within itself?
by Improv (Pilgrim) on Apr 17, 2003 at 16:46 UTC
    One solution is to fork and have the parent exit.