Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by reyjrar (Hermit)
on Nov 15, 2000 at 04:04 UTC ( #41683=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://41683]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2021-04-18 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?