Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
Just another Perl shrine
 
PerlMonks  

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

by reyjrar (Hermit)
on Nov 14, 2000 at 23:04 UTC ( [id://41683]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 merlyn (Sage) on Nov 14, 2000 at 23: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 japhy (Canon) on Nov 14, 2000 at 23: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 Fastolfe (Vicar) on Nov 15, 2000 at 14: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 jeroenes (Priest) on Nov 15, 2000 at 14: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 Anonymous Monk on Apr 16, 2003 at 17:26 UTC
    REAT'

    Originally posted as a Categorized Answer.

Re: Is it possible to background a perl script from within itself?
by belg4mit (Prior) on Apr 16, 2003 at 22: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.
      One solution is to fork and have the parent exit.
      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?
Re: Is it possible to background a perl script from within itself?
by jettero (Monsignor) on Jun 05, 2007 at 08:54 UTC

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

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://41683]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.