Hello Monks!

I need some help with trying to find a way to have a single script complete two functions at the same time and then compile this into an executable... if that is at all possible.

The first point of the script is to constantly monitor a IP address and port for data and when any data is presented on that port number the script parses it and sends an alert if it finds the word "Emergency". This portion of the project is working fine at the moment.

The second function is for the script to send an "I'm alive" email every 30 mins or so but I can't seem to figure out how I can do this within the same script as the above even with subroutines in place. Currently I just have a simple sleep subroutine within the main script but it is not getting to that because I believe the main script is not allowing it to get that far.

My code (ugly as it is) is below. Any help appreciated!

#!/usr/bin/perl use strict; use IO::Socket; use POSIX; use warnings; use MIME::Lite; use Config::Simple; use Win32(); sub logMonitor{ # Reading configuration parameters from the config.ini file my $cfg = new Config::Simple(); $cfg->read('config.ini'); my $HOST = $cfg->param("pbx"); my $COMPANY = $cfg->param("company"); my $COMMUNITY = $cfg->param("community"); my $PORT = $cfg->param("port"); for(;; ){ my $sock = new IO::Socket::INET(PeerAddr => $HOST, PeerPort => + $PORT,Proto => "tcp",) or die "Cannot connect to PBX at address: $HOST port: $PORT: $ +!"; while (<$sock>) { s/^\0+//; # Remove leading null characters print $_; chomp ($_); if ($_ =~m"Emergency Call From Extension") { filePrint(); } } } } #End of monitor subroutine sub filePrint{ #write data to file with date and time stamp my $data = $_; print $data; my $file = strftime '%Y-%m-%d_%H-%M-%S.txt', localtime; if (-f $file){ open (my $fh,'>>', $file); print $fh "$_"; close $file; } else { open (my $fh,'>', $file); print $fh "$_"; close $file; } }# End of filePrint routine sub keepAlive{ while () { sleep 20; print " It's alive!!!"; } } #print disclaimer and wait for "y" to continue or exit my $disclaimer = "disclaimer.txt"; open(my $fh, '<:encoding(UTF-8)', $disclaimer) or die "Could not open file '$disclaimer' $!"; while (my $row = <$fh>) { chomp $row; Win32::MsgBox($row); } print "\nDo you accept this disclaimer and continue at your own risk?( +Y/N)?"; # first question yes/no my $input = <STDIN>; chomp $input; if ($input =~ m/^[Y]$/i){ # Start of processing logMonitor(); #Open the log monitoring subroutine keepAlive(); #start the alive email message }

In reply to Timed event within a script by bajangerry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.