Re: How do I run an Infinite loop with a CGI?
by rob_au (Abbot) on Nov 08, 2002 at 12:03 UTC
|
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"' | [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
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
| [reply] |
|
|
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"'
| [reply] |
Re: How do I run an Infinite loop with a CGI?
by graq (Curate) on Nov 08, 2002 at 13:53 UTC
|
There are some subtle differences between infinite loops and daemons, but you can (at times) take that obvious route.
It sounds like you want a daemon.
If you merely want the daemon to do something on request of a CGI script, then you can write the daemon to take care of itself (from an OS point of view) and just have it running all the time. Serving the answers as and when you need them.
If you want to be able to start the daemon from the web, I would still look at why this is, and consider something paranoid (like I do). Which is a cron-activated script that checks that my daemons are running. I even have a daemon that troubleshoots 3 other daemons (won't explain that now).
Otherwise, listen to rob_au :)
<a href="http://www.graq.co.uk">Graq</a> | [reply] |
Re: How do I run an Infinite loop with a CGI?
by paulbort (Hermit) on Nov 08, 2002 at 16:25 UTC
|
From the brute force and stupidity department: split the two and use an interface to control starting and stopping the daemon. (1) make the socket listener a completely separate program (sounds like it already is) that starts and stops listening on the socket depending on some outside control (like listening on a second socket, or watching for files in its home directory, or catching a signal, or something.) (2) make the CGI send the start and/or stop signals to the daemon. This also solves some security problems.
rob_au's suggestion is also entirely valid, this is just TIMTOWTDI, and possibly more portable to strange environments like Win32.
-- Spring: Forces, Coiled Again!
| [reply] |
Re: How do I run an Infinite loop with a CGI?
by fglock (Vicar) on Nov 08, 2002 at 11:48 UTC
|
Have you tried:
` other_script.pl & `;
| [reply] [d/l] |
Re: How do I run an Infinite loop with a CGI?
by elwarren (Priest) on Nov 08, 2002 at 16:53 UTC
|
Are you trying to start this on a hosting provider that only allows ftp and no telnet? Sometimes places like this do not allow you to execute code like this unless you're logged in (wtmp entry.)
Have you tried it on any other servers? | [reply] |
Re: How do I run an Infinite loop with a CGI?
by valdez (Monsignor) on Nov 08, 2002 at 18:39 UTC
|
You didn't post some code, so I can only guess:
I want to be able to start the script from a CGI script through a browser. All my attempts at doing this so far result in the browser timing out. The script will be started but my status message will not be displayed.
did you close STDOUT in your child process?
Today there is another node on this subject: Understanding fork. There is also a column by merlyn: Watching long processes through CGI. Where is merlyn when you need him? :)
Ciao, Valerio
| [reply] |