CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Pausing one peace of code while another continues.

 



brian.hayes
User

Jan 12, 2000, 12:42 PM

Post #1 of 11 (1519 views)
Pausing one peace of code while another continues. Can't Post

Hello again.


My goal is to have a piece of code within a perl script that stays live "Always Running", and have it branch of to read in a file and if there is an error in the file send a page alert to someone "ME".

There are several things going on here, THere is a program that runs nighty proccess and writes to file with out put."DATE GOOD or BAD and TIME". THis will alway remain true and it runs nightly.

I have writen the main script. It is a small web server that does nothing but out put HTML based on the internal readings of its own error log, and sub scripts to do all the file checking and even one to send a page via Pager companies web site using LWP::UserAgent.

What I need know is this small web server code to continue waiting for request, which it is currently doing, but with the ability to sleep on a sub for so long before checking or running the sub again "Read the other programs out put file for errors".

OR if you know of a good alert package software preferalbly PERL, because I finaly got Perl into our production environment, whith me leading the way "Scarry".

Any Clues?

Brian Hayes


brian.hayes
User

Jan 12, 2000, 1:08 PM

Post #2 of 11 (1518 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Sorry..

Something like this..


Start main..
create/listen on tcp port XXXX,IP X.X.X.X
select(SOCK); Etc......

sub monitor{
# while is always true when tcp port is up.
while(1){
open FILE,"Path to file" or die "$!";
@in = <FILE>
foreach (@in){
&check_file($_);

} ## I need to do this every hour.
close FILE;
sleep(5*60); ## This stops the entire program. Not good..Need to continue to rest of program.

}
}

sub check_file{

open INTERNAL,"Path to internal log";

if($_ =~/^file is good/i){
print INTERNAL, "Good", "\n";
elsif ($_ =~/^File is bad/i){
print INTERNAL, "Bad", "\n";
Alert code here...
} else {
print INTERNAL, "Bad", "\n";
Alert code here.. Asume bad if no match.
}

close INTERNAL;
return 0;
}


Hopfully this is making scence...

Thanks again...

Brian Hayes


japhy
Enthusiast

Jan 12, 2000, 4:39 PM

Post #3 of 11 (1521 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

You want to fork(), as demonstrated here:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

if ($pid = fork) { monitor() }
elsif (not defined $pid) { die "Can't fork: $!" }

# continue code as normal
# ...
sub monitor {
while (1) {
# ...
sleep 3600; # 1 hour
}
exit; # just in case something gets you out of the loop
}
</pre><HR></BLOCKQUOTE>

That should work for you. Read perlfunc and perlipc for information on fork().


brian.hayes
User

Jan 12, 2000, 5:20 PM

Post #4 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Thanks japhy,


Now the real problem is that I have tested this to be true on my linux box "At Home", with one exeption....This is not a supported function under Windozz "At work". I will need this to work under this platform until I can get a Unix server......This is just another reason to stay Unix of any flavor...

Thanks again.

Brian Hayes


japhy
Enthusiast

Jan 12, 2000, 6:44 PM

Post #5 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Oh dear. You are "screwed", pretty much. I'm at a loss. I don't know if there's a module in the Win32:: hierarchy that can help you. I hope so.


Jasmine
Administrator / Moderator

Jan 12, 2000, 8:01 PM

Post #6 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Now forgive me if this seems incorrect -- I'm the first to admit that I'm clueless when it comes to Perl for Win32.

It sounds like you're wanting to run an NT equivalent of cron, a *nix daemon that runs according to a schedule you define.

If this is the case, you may wish to consider setting up a "cron emulator" Perl program as an NT service.

Alternatively, here's a snippet of code I found at ActiveState.

The article suggests that the sleep function may be an appropriate scheduling tool.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$seconds = 180;
sub action;
while(1) {
&action;
sleep($seconds);
}
</pre><HR></BLOCKQUOTE>

I hope some of this helps. If it's confusing (more harmful than helpful), my apologies... I tried Smile


Jasmine
Administrator / Moderator

Jan 12, 2000, 8:05 PM

Post #7 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Oops .. I just noticed in your previous post that sleep causes the program to stop... please ignore that code snippet Smile


brian.hayes
User

Jan 13, 2000, 4:28 AM

Post #8 of 11 (1518 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Just as I thought...I will need to write multiple programs to get this one done then use the built in job function on NT.. Well Until I get a unix box inhouse...


Thanks,

Brian Hayes



Jasmine
Administrator / Moderator

Feb 3, 2000, 8:29 AM

Post #9 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Found something that may be useful (from perl-win32-users@activestate.com) ...

Win32::Process to Emulate fork()
True forking is not possible on Win32 platforms, but this HTTP server example does some fancy work to get around this. Gisle Aas' HTTP: aemon leaves the forking to the developer, but since Win32 Perl doesn't currently support fork, implementing an HTTP server as it was intended to run is impossible. You cannot open a handle in one process and then expect to access it in another process, but because STDIN gets passed between one process and another, this code can successfully fake a fork by closing STDIN and redirecting the socket to STDIN and then creating the agent process.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

##########################################
# Copyright 1996-1999, Digital Paper
#
# This source code is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Listener.pl
#

use HTTP: aemon;
use HTTP::Status;
use Win32::Process;
use Getopt::Long;

#########################
# Listener
#########################
GetOptions( "port=s" => \$port,
"listen:s" => \$listen);

$listen &#0124; &#0124;= 5;
$host = hostip();
my $listener = HTTP: aemon->new(LocalAddr => $host,
LocalPort => $port,
Listen => $listen) &#0124; &#0124;
exitGracefully("Listener could not be created on $host:$port, listen queue=$listen\n");

print "[Listener] Waiting for Requests on ".$listener->url."\n";

for(;$c = $listener->accept; $c->close)
{
print "\n[Listener]****ACCEPTED CONNECTION****\n";
binmode $c;

print "[Listener] Spawning an Agent\n";
spawn($c);
}
$c = undef;

#**************************************************
# SUPPORT FUNCTIONS
#**************************************************

sub spawn
{
my $new = shift;

# You better define this!
my $somedirectory = '';

open(SAVEIN, "<&STDIN");

my $socket_no = $new->fileno;

open(STDIN, "<&".$socket_no) &#0124; &#0124;
exitGracefully("Could not open STDIN as new socket\n", 1);

$new->close;

my $proc;
my $cmd = "$somedirectory\\perl.exe $somedirectory\\Agent.pl";
my $app = "$somedirectory\\perl.exe";
my $inherit = 1;

my $process_params = &NORMAL_PRIORITY_CLASS;

Win32::Process::Create($proc, $app, $cmd, $inherit,
$process_params, '.')
&#0124; &#0124; do {
&win32_errmsg;
exitGracefully("Failed to execute: $app $cmd; $win32_errmsg\n", 1);
};

open(STDIN, "<&SAVEIN");
close(SAVEIN);
}

#---------------------------------------------------------------------

sub win32_errmsg
{
$win32_errmsg = Win32::FormatMessage(Win32::GetLastError());
}

#---------------------------------------------------------------------

sub exitGracefully
{
my $msg = shift;
my $noexit = shift;

$ss->close if $ss; undef($ss);
print "[Listener] $msg\n";

exit unless $noexit;
}

#---------------------------------------------------------------------

sub hostip
{
use Sys::Hostname;
use IO::Socket;

my $host = @_ &#0124; &#0124; hostname();
return inet_ntoa(inet_aton($host));
}
</pre><HR></BLOCKQUOTE>


brian.hayes
User

Feb 3, 2000, 7:01 PM

Post #10 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Thanks for this information :-)

This is what I found at activestate:

* Support for fork()

The lack of fork() has been the biggest portability concern for Windows users who would like to leverage the vast body of reusable Perl code available on CPAN. ActivePerl 5.6 will emulate fork() at the interpreter level, making it possible to run scripts that have until now required a Unix-like platform.


I will test this to see what the performance is like or I could wait for the new build :-(


Thanks,

Brian Hayes


Jasmine
Administrator / Moderator

Feb 3, 2000, 7:56 PM

Post #11 of 11 (1519 views)
Re: Pausing one peace of code while another continues. [In reply to] Can't Post

Good deal ... let us know how it turns out Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives