Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

( #41683=categorized question: print w/ replies, xml ) Need Help??
Contributed by reyjrar on Nov 15, 2000 at 04:04 UTC
Q&A  > programs and processes


Description:

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!

Answer: Is it possible to background a perl script from within itself?
contributed by Fastolfe

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.
Answer: Is it possible to background a perl script from within itself?
contributed by japhy

I believe this will do the trick:

#!/usr/bin/perl print "Now backgrounding your process...\n"; fork and exit; # rest of code goes here
Answer: Is it possible to background a perl script from within itself?
contributed by jeroenes

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.

Answer: Is it possible to background a perl script from within itself?
contributed by belg4mit

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2014-11-29 08:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (204 votes), past polls