Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by belg4mit (Prior)
on Apr 17, 2003 at 02:51 UTC ( [id://251113]=note: print w/replies, xml ) Need Help??


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

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.
  • Comment on Re: Is it possible to background a perl script from within itself?

Replies are listed 'Best First'.
Re: Answer: Is it possible to background a perl script from within itself?
by hbo (Monk) on Apr 18, 2003 at 06:09 UTC
    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...
    
Re: Answer: Is it possible to background a perl script from within itself?
by hbo (Monk) on Apr 18, 2003 at 06:18 UTC
    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: Answer: Is it possible to background a perl script from within itself?
by Improv (Pilgrim) on Apr 17, 2003 at 16:46 UTC
    One solution is to fork and have the parent exit.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-18 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found