http://www.perlmonks.org?node_id=41683

reyjrar has asked for the wisdom of the Perl Monks concerning the following question: (programs and processes)

Is it possible to background a perl script from within itself, so that it starts like a daemon, i.e. like a generic equivalent of & on unix?
> ./myserver Server starting ... bound to port 4000!

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Is it possible to background a perl script from within itself?
by Fastolfe (Vicar) on Nov 15, 2000 at 19:18 UTC
    I typically use a variation of this 'daemonize' code taken from perlipc. This sets up the script's environment and disassociates it from the caller entirely.
    sub daemonize { chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/nul +l: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; }
    You could also open STDOUT to point to, say, a log file here. Or a tied filehandle passing its data to syslog.
Re: Is it possible to background a perl script from within itself?
by japhy (Canon) on Nov 15, 2000 at 04:40 UTC
    I believe this will do the trick:
    #!/usr/bin/perl print "Now backgrounding your process...\n"; fork and exit; # rest of code goes here
Re: Is it possible to background a perl script from within itself?
by jeroenes (Priest) on Nov 15, 2000 at 19:49 UTC

    See this thread for info on executing backgrounding a script.
    Specialized modules exist for this kind of problem:

    I was amazed by the length of the 'detaching' sequence.
    You need Net::daemon if you want to claim a socket, otherwise Proc::daemon should suffice.

Re: Is it possible to background a perl script from within itself?
by belg4mit (Prior) on Apr 17, 2003 at 02:51 UTC
    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.
      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...
      
      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?
      One solution is to fork and have the parent exit.
Re: Is it possible to background a perl script from within itself?
by merlyn (Sage) on Nov 15, 2000 at 04:40 UTC
    Yes. You can find the answer by looking in the FAQ, or by looking at perlman:perlipc.

    Originally posted as a Categorized Answer.

Re: Is it possible to background a perl script from within itself?
by jettero (Monsignor) on Jun 05, 2007 at 12:54 UTC

    Re: Is it possible to background a perl script from within itself?

    Originally posted as a Categorized Answer.

Re: Is it possible to background a perl script from within itself?
by Anonymous Monk on Apr 16, 2003 at 21:26 UTC
    REAT'

    Originally posted as a Categorized Answer.