Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by reyjrar (Hermit)
on Nov 15, 2000 at 04:04 UTC ( #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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?
    Corion idly wonders about creating a series of HTTP requests. There seems to be no framework to generate a series of HTTP requests, like "try all these requests"
    [Corion]: This is vaguely inspired by that list of "nasty" strings, which I'd like to replace among HTTP parameters, just to see whether the application crashes, but also for easy downloading of a list of URLs etc
    [Corion]: Maybe I'll just conoct something using Algorithm::Permute to create an (OO) generator for such requests, or a simple function.
    [Corion]: For testing I imagine one would want to test a random sampling of such "bad"/"unexpected" requests, while for downloading, one would want to generate them all in order, but not necessarily as a huge list

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2017-01-16 15:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you watch meteor showers?




      Results (151 votes). Check out past polls.