in reply to How do I run an Infinite loop with a CGI?

The best solution would be to rewrite the Perl socket server script to run as a daemon process; that is, as a program that runs continuously for the purpose of handling periodic service requests.

True to the spirit of there being more than one way to do it, there a number of ways by which a Perl script can detach from its controlling process and act as a daemon process. One of the easiest methods is through the use of the Proc::Daemon module from CPAN - For example:

use Proc::Daemon; Proc::Daemon::Init; . . . # The rest of the script follows

This code will fork a child and exit the parent process, detaches from the controlling terminal, forks a second process and detaches the parent process (preventing the potential of acquiring a controlling terminal), changes to the root directory, clears the file creation mask and closes all open file descriptors - This procedure is the same as that described by W. Richard Stevens in Advanced Programming in the UNIX Environment.

 

perl -e 'print+unpack("N",pack("B32","00000000000000000000000111011011")),"\n"'

Replies are listed 'Best First'.
Re^2: How do I run an Infinite loop with a CGI?
by tadman (Prior) on Nov 08, 2002 at 16:55 UTC
    Starting a process from within the Web server environment should probably be done with system and not fork or exec. It's true that in many cases, calling system results in a fork, but it is likely to work in cases where the others do not.

    If your "daemon" script is properly behaved, and returns usable exit codes, you can do something like this:
    my $error = system("/usr/local/bin/my.daemon"); if ($error) { print "<I>Problem launching server. (Error code $error)</I>\n"; } else { print "<I>Server started.</I>\n"; }
    Forking off a copy of the Web server run-time environment and all of its associated baggage is rather inefficient, since you'd likely use very little of this. Starting a new process means that your "daemon" script can begin with a clean slate, no open file handles, allocated memory, and such.
Re: Re: How do I run an Infinite loop with a CGI?
by JamieD (Novice) on Nov 08, 2002 at 22:38 UTC
    Thanx for all the responses, I now have this working using the Proc Daemon method and using system() with the error trapping.

    I now have annother question.

    Is it possible to now call functions in the running daemon from annother cgi script?

    For instance a stop() function.

    Thanx
      Once again there are many ways to do this - A good starting point would be to have a look at the Perl interprocess communication manpage (perlipc). This page provides a good overview of available methods by which processes can communication between one another and call internal functions including signals, named pipes, sockets and SysV IPC.

      It would also be worth having a look through CPAN, in particular the Networking Devices and IPC section, to see what is available for this type of communication.

       

      perl -e 'print+unpack("N",pack("B32","00000000000000000000000111011100")),"\n"'