I hadn't realized that while loops were more efficient than for loops... I just liked the english-like syntax of the foreach loop :) I also usually try to avoid the $_ variable because early in my perl career I always had trouble keeping track of when it was modified.

Anyway, stylistic preferences aside... I made the foreach -> while loop changes you suggested and am getting the same results. With both the original and modified versions of the code the hangup point has moved to just after Marker 2.

I also decided to leave things alone and let the code run its course... and it does decide to continue after about 30 seconds. Which might at least be acceptable as this script will only be running as an hourly cronjob.

I'm also not sure if this really is a memory issue. Here's the output from top while the script is hung up:

PID COMMAND %CPU TIME #TH #PRTS #MREGS RPRVT RSHRD RSIZE VSIZE 1279 perl 19.6% 0:02.28 1 10 21 524K 1.42M 1.62M 2.92M

As you can see, perl isn't using up much memory at all. For those of you wondering about the strange top format... this is on my ibook. Here's the output from top on a linux box:

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND 16974 root 25 0 1696 1696 1128 R 51.5 0.1 0:02 ip_update.pl

Well, in the middle of writing this I finally understood what you meant by movind the print DHCPDCONF line and the temporary file... so I made those adjustments and, of course there is no hang. I'm still not sure why it was hanging in the first place, but I guess that is a moot point now.

Thanks for your help :)

Updated Code:

#!/usr/bin/perl use strict; ##################################################### # Update MAC-IP pairings for all registered devices # ##################################################### my %devices; # Open dhcpd.conf and initialize the %devices hash with all # registered modems and cpes open (DHCPDCONF, "dhcpd.conf") or die "Cannot read from file: $!"; while (<DHCPDCONF>) { $devices{lc($1)} = '000.000.000.000' if (/#.*#.*#.*# Modem MAC: (\w+:\w+:\w+:\w+:\w+:\w+)/ or /subclass ".*" 1:(\w+:\w+:\w+:\w+:\w+:\w+)/); } close DHCPDCONF; # open leases file and match mac addresses in %devices with active ips open (LEASES, "dhcpd.leases"); my ($current_ip, $state); while (<LEASES>) { $current_ip = $1 and next if /^\s*lease (\d+.\d+.\d+.\d+)/; $state = $1 and next if /^\s*binding state (\w+)/; $devices{lc($1)} = $current_ip and print lc($1) . " - " . $devices{lc($1)} . "\n" and next if (/^\s*hardware ethernet (\w+:\w+:\w+:\w+:\w+:\w+)/ and $state eq 'active' and exists $devices{lc($1)}); } close LEASES; # substitute old ip with current value open (DHCPDCONF, "dhcpd.conf") or die "Cannot read from file: $!"; open (DHCPDCONFNEW, ">dhcpd.conf.new") or die "Cannot create file: $!" +; while( <DHCPDCONF>) { my $device = lc($1) if /#.*#.*#.*# Modem MAC: (\w+:\w+:\w+:\w+:\w+ +:\w+)/; s/(#.*#.*# Modem IP: ).*( # Modem MAC: $device)/$1$devices{$device +}$2/i; print DHCPDCONFNEW; } close DHCPDCONF; close DHCPDCONFNEW; system("cp dhcpd.conf.new dhcpd.conf"); ######## Update LDAP database # not implemented yet.

In reply to Re: Re: script hangs up when reading in file by tombmbdil
in thread script hangs up when reading in file by tombmbdil

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.