Scott, Joshua wrote:
> I'm new to Perl programming.  I'm trying to launch a process from within 
> a CGI script and then continue running the script to complete the web 
> page.  Here is what I'm trying to run.
>  
> $status=system("/usr/bin/program -x -x -x &");
>  
> Whenever this is executed, the script goes into a DEFUNCT mode until the 
> child program is either completed, or killed.  I'd like to have the 
> script just start the process in background mode and return to the script.

These man page refs may help:

 From perlipc man page:

   Background Processes

     You can run a command in the background with:

         system("cmd &");

     The command's STDOUT and STDERR (and possibly STDIN, depending on your
     shell) will be the same as the parent's. You won't need to catch SIGCHLD
     because of the double-fork taking place (see below for more details).

   Complete Dissociation of Child from Parent

     In some cases (starting server processes, for instance) you'll want to
     completely dissociate the child process from the parent. This is often
     called daemonization. A well behaved daemon will also chdir() to the root
     directory (so it doesn't prevent unmounting the filesystem containing the
     directory from which it was launched) and redirect its standard file
     descriptors from and to /dev/null (so that random output doesn't wind up on
     the user's terminal).

         use POSIX 'setsid';

         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/null: $!";
             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: $!";
         }

     The fork() has to come before the setsid() to ensure that you aren't a
     process group leader (the setsid() will fail if you are). If your system
     doesn't have the setsid() function, open /dev/tty and use the "TIOCNOTTY"
     ioctl() on it instead. See the tty(4) manpage for details.

 From perlfaq8:

   How do I fork a daemon process?

     If by daemon process you mean one that's detached (disassociated from its
     tty), then the following process is reported to work on most Unixish
     systems. Non-Unix users should check their Your_OS::Process module for other
     solutions.

     *   Open /dev/tty and use the TIOCNOTTY ioctl on it. See the tty(4) manpage
         for details. Or better yet, you can just use the POSIX::setsid()
         function, so you don't have to worry about process groups.

     *   Change directory to /

     *   Reopen STDIN, STDOUT, and STDERR so they're not connected to the old
         tty.

     *   Background yourself like this:

             fork && exit;

     The Proc::Daemon module, available from CPAN, provides a function to perform
     these actions for you.


-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to