xjlittle has asked for the wisdom of the Perl Monks concerning the following question:

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"; } }

Replies are listed 'Best First'.
Re: Using fork with subroutines
by GotToBTru (Prior) on Dec 10, 2014 at 14:18 UTC

    No test data, and a script so dependent on the local network and environment that nobody other than you could possibly execute it.

    I would suggest adding diagnostic print statements to the script so you can check to see if global variables have the values you expect in the client instances and subroutines. Not sure how the debugger works with fork.

    1 Peter 4:10
Re: Using fork with subroutines
by QM (Parson) on Dec 10, 2014 at 14:18 UTC
    The subs only see one $ssh value, because it isn't passed in.

    Other comments:

    What happens when any of the remote commands fail? Is remote sudo enabled?

    Why all the forks? Why not just fork in the for loop, and then do all the system and sub calls?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Using fork with subroutines
by jfroebe (Parson) on Dec 10, 2014 at 20:54 UTC
Re: Using fork with subroutines
by Anonymous Monk on Dec 10, 2014 at 14:51 UTC
    I tried to run it through perltidy to make it readable, but there're some syntax errors. Please fix your script.