Good Morning Monks!

I am in need of your wisdom. I have the below script which appears to work properly when only 1 element is in the array. However when I add a 2nd element to the array the script attempts to repeat the copy and gunzip on the 1st element rather than moving on to the 2nd element.

May you please impart some wisdom as my logic is apparently lacking in this matter.

Thanks!

#!/usr/bin/perl -w use strict; use File::Find (); use Net::OpenSSH; use File::Copy; my @testbox = qw( mail1 mail2 ); my $dir = "/spool/stats"; my $ckdir = "if [ ! -d $dir ]; then sudo mkdir -p $dir && sudo chmod - +R 777 $dir; else exit; fi"; my $getbouncelogs = "find /var/archive '(' -name 'bounce'* ')' -mtime +-7 -mtime +0 |xargs -I {} cp -a {} $dir"; my $getdeliverylogs = "find /var/archive '(' -name 'delivery'* ')' -mt +ime -7 -mtime +0 |xargs -I {} cp -a {} $dir"; for my $host(@testbox){ my $ssh = Net::OpenSSH->new("$host"); $ssh->system("$ckdir"); #Remove the old logs print "removing old logs on $host\n"; $ssh->system("rm -f $dir/*") or die "$!"; &getlogs; &gunzip; } sub getlogs { #Get the most recent 7 days my $pid = fork; if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # client process print "Client copy delivery logs starting...\n"; $ssh->system("$getdeliverylogs"); print "Client copy delivery logs sterminating\n"; exit 0; } else { # parent process print "Parent process copy bouncelogs, waiting for child...\n"; $ssh->system("$getbouncelogs"); waitpid $pid, 0; } print "Parent copy bouncelogs process started after child has finished +\n"; } } sub gunzip { #gunzip the logs my $pid = fork; if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # client process print "Client gunzip delivery logs starting...\n"; $ssh->system("gunzip $dir/delivery*.gz"); print "Client gunzip delivery logs terminating\n"; exit 0; } else { # parent process print "Parent process gunzip bounce logs, waiting for child...\n"; $ssh->system("gunzip $dir/bounce*.gz"); waitpid $pid, 0; } print "Parent gunzip bouncelogs process started after child has finish +ed\n"; } }

In reply to Using fork with subroutines by xjlittle

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.