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

Backgrounding a Program

by Anonymous Monk
on Nov 16, 2001 at 00:30 UTC ( [id://125659]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do you get a program to background itself in perl? Thanks, Greg

Replies are listed 'Best First'.
Re: Backgrounding a Program
by tadman (Prior) on Nov 16, 2001 at 00:53 UTC
    As c-era and broquaint say, the key is fork. This will make a "clone" of your program that is not attached to the terminal, and thus, the main program can exit and leave you with the shell prompt.

    This is all fine and well, but make sure you are aware of some key issues:
    • Don't fork right away. Make sure your program is "up and running" before the big fork call. This way you can report any errors, such as those with command line parameters or configuration files, to the console. If you fork and then warn or die, the output can't be captured. If this is called from a "crontab", you won't even see the error. So, configure, warn/die if required, then fork.
    • Sometimes you don't want the program to fork. Including an option that prevents this, or alternatively, enables this, would be helpful when trying to debug. Otherwise your debugger might stop on the fork call, leaving your clone to run wild and free.
    • If your program is a "daemon" type process, try and blend in by following standard conventions. This might mean making an "/etc/rc.d/"-compatible (SysV-Init) RC boot file, creating a PID file in /var/run, and so forth. This will help you figure out if your thing is running already, and help manage start/stop operations.
    Just food for thought.
Re: Backgrounding a Program
by c-era (Curate) on Nov 16, 2001 at 00:38 UTC
    Here's one way to do it:
    my $pid = fork; exit if ($pid); # Your code here
Re: Backgrounding a Program
by broquaint (Abbot) on Nov 16, 2001 at 00:41 UTC
    You can't unfortunately, but this is a 'limit' of OSes, and not of perl. So you probably either want to take advantage of multi-tasking in *nix-type shells or fork() off at the beginning of your program.
    prompt> perl source.pl &
    or with perl
    use strict; fork() and exit(0); print "Now in the child process\n";
    The perl example is a little hackish, but there's plenty of documentation about to explain it all.
    HTH

    broquaint

Re: Backgrounding a Program
by jlongino (Parson) on Nov 16, 2001 at 01:21 UTC
    For Windows platforms you might also want to look at Win32::Process

    --Jim

Re: Backgrounding a Program
by Erik Hensema (Sexton) on Nov 16, 2001 at 03:01 UTC

    On Unix systems you can use this:

    use POSIX; sub daemon { if($pid = fork) { # Parent exit 0; } elsif (defined $pid) { # Child setsid(); # detach session do_stuff(); # do some usefull stuff } else { die "Cannot fork: $!\n"; } }
      Just a quick note, the usage of POSIX doesn't necessarily exclude Windows-based systems with Windows NT and 2000 being POSIX-compliant (although I should note, there were a couple of questions on some functions with Windows NT) - The usage of POSIX does however preclude Windows 9x, for which Win32::Process offers a better solution with regard to setting priority classes and process suspension and execution.

       

      Ooohhh, Rob no beer function well without!

Re: Backgrounding a Program
by Anonymous Monk on Nov 16, 2001 at 05:50 UTC

    If you want your process to properly act as a daemon, then the simplest method is to use the Proc::Daemon module:

    #!/usr/bin/perl -w use strict; use Proc::Daemon; Proc::Daemon::Init; # your code goes here __END__

    This module takes care of all the little things necessary for proper daemonization: as per the docs:

    1. Forks a child and exits the parent process.
    2. Becomes a session leader (which detaches the program from the controlling terminal).
    3. Forks another child process and exits first child. This prevents the potential of acquiring a controlling terminal.
    4. Changes the current working directory to "/".
    5. Clears the file creation mask.
    6. Closes all open file descriptors.
Re: Backgrounding a Program
by ehdonhon (Curate) on Nov 16, 2001 at 03:51 UTC
    I just asked a very similar question the other day. You can find a long discussion here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found