#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Barbados
    Posts
    147
    Rep Power
    59

    No output to screen when calling a perl script from another perl script


    Hi,

    I have a perl scripts (A) that when run will output information (mainly for logging and debugging) to the screen as it runs.

    I created another script B that runs some commands and then calls A. I can see the output from A, but nothing from B.

    I am using the system('B.pl') command to call B from A.


    Any assistance would be appreciated.


    Thansk

    Steve
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    You are saying contradictory things. Are you calling B from A or A from B?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Barbados
    Posts
    147
    Rep Power
    59
    Originally Posted by Laurent_R
    You are saying contradictory things. Are you calling B from A or A from B?
    Sorry about that [brain fog], lets start again.

    I can run A.pl on it's own and see output, but when I call it from B.pl, I do not get any output.


    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,873
    Rep Power
    1225
    Please post your scripts.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Barbados
    Posts
    147
    Rep Power
    59
    Originally Posted by FishMonger
    Please post your scripts.
    This is A.pl
    PHP Code:

    # use modules
    use File::Slurp;
    use 
    XML::Simple;
    use 
    Data::Dumper;
    use 
    DBI;

    # MySQL CONFIG VARIABLES
    $host "localhost";
    $database "dashboard";
    $user "root";
    $pw "sysadmin";


    print 
    "Opening Data.txt \n\r";
    open (MYFILE'data.txt');
    while (<
    MYFILE>) {
         
    chomp;
         
    my $file $_.".results";
        
    my $text read_file($file) ;
        print 
    "Processing file $file \n\r";
        
    #die();

        
    my $start '<equipment.InterfaceAdditionalStats>';
        
    my $end '</equipment.InterfaceAdditionalStats>';
        
    my $startpos index($text$start);
        
    my $endpos index($text$end);
        
    $length $endpos $startpos length($end);
        
    my $fragment =  substr $text$startpos$length;
        
    #//print "Fragment - $fragment \n\r";

        # create object
        
    $xml = new XML::Simple;

        
    # read XML file
        
    $stats $xml->XMLin("$fragment");

        
    $rxOctets $stats->{receivedTotalOctets};
        
    $txOctets $stats->{transmittedTotalOctets};
        
    $timeCaptured $stats->{timeCaptured};
        
    $periodicTime $stats->{periodicTime};
        
    $port $stats->{displayedName};  #//Comes back as "Port x/y/z"
        
    @portPieces split(" ",$port);      #//Explode splits results
        
    $port $portPieces[1];                  #//Stores last part into variable
        
    $ipAddress $stats->{monitoredObjectSiteId};
        
    $node $stats->{monitoredObjectSiteName};

        print 
    "tc - $timeCaptured \n\r";
        print 
    "Processing $port on $node \n\r \n\r";

        
    #DATA SOURCE NAME
        
    $dsn "dbi:mysql:$database:localhost:3306";

        
    # PERL DBI CONNECT
        
    $connect DBI->connect($dsn$user$pw);

        
    # Determine the portId of this port
        # PREPARE THE QUERY
        
    $sqlPortId "SELECT portid from ports where node like '$node' and portName like '$port'";
        print 
    "\n$sqlPortId";
        
    $resPortId $connect->prepare($sqlPortId);
        
    $resPortId->execute();

        
    # LOOP THROUGH RESULTS
        
    while (@ports $resPortId->fetchrow_array()){
            
    $portId $ports[0];

            
    #Retrieve timeCaptured from data_new table and compare to time variable
            #If the same, do nothing
            #If different, move data from data_new into data_old

            
    $sqlTime "SELECT timeCaptured from data_new WHERE portId = '$portId'";
            
    $resTime $connect->prepare($sqlTime);    
            
    $resTime->execute();
            
    $numTime $resTime->rows;

            
    #get data and time
            
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
            
    $year += 1900;
            
    $datadatetime "$year-$mon-$mday $hour:$min:$sec \n\r";

            if (
    $numTime 0) {
                while (@
    times $resTime->fetchrow_array()){
                    
    $oldTimeCaptured $times[0];
                }
                print 
    "Times - $timeCaptured (new) - $oldTimeCaptured (old)\n\r\n\r";
                
                if (
    $timeCaptured $oldTimeCaptured) {  #New data, so move data to data_old
                    
    print "Processing new data ...... \n\r\n\r";
                    
    $sqlnewData "select * from data_new where portId = '$portId'";
                    
    $resnewData $connect->prepare($sqlnewData);
                    
    $resnewData->execute;
                    
                    while (@
    data $resnewData->fetchrow_array()){
                        
    $oldrxOctets $data[2];
                        
    $oldtxOctets $data[3];
                        
    $oldtimeCaptured $data[4];
                        
    $oldperiodicTime $data[5];
                        
    $oldport $data[6];
                        
    $oldnode $data[7];
                        
    $oldipaddress $data[8];
                        
    $olddatadatetime $data[9];
                    }    
                    
                    
                    
    #Check to see if this is an existing port in data_old
                    
    $sqlCheckOld "select * from data_old where portId = '$portId'";
                    print 
    "Old check - $sqlCheckOld \n\r\n\r";
                    
                    
    $resCheckOld $connect->prepare($sqlCheckOld);
                    
    $resCheckOld->execute;
                    
    $numCheckOld $resCheckOld->rows;
                    
                    
                    
                    if (
    $numCheckOld 0) {
                        
    $sqloldData "update data_old set rxOctets = '$oldrxOctets', txOctets = '$oldtxOctets', timeCaptured = '$oldtimeCaptured', periodicTime = '$oldperiodicTime', datadatetime = '$olddatadatetime' where portid like '$portId'";
                        print 
    "Old data updated - $sqloldData \n\r \n\r";
                        
    $resoldData $connect->prepare($sqloldData);
                        
    $resoldData->execute();
                    } else {
                        
    $sqloldData "insert into data_old (rxOctets, txOctets, timeCaptured, periodicTime, portId, datadatetime) values ('$oldrxOctets', '$oldtxOctets', '$oldtimeCaptured', '$oldperiodicTime', '$portId', '$olddatadatetime')";
                        print 
    "Old data added - $sqloldData \n\r \n\r";
                        
    $resoldData $connect->prepare($sqloldData);
                        
    $resoldData->execute();
                    }
                
                    
    #Write new data into data_new    
                    
    $sqlnewData "update data_new set rxOctets = '$rxOctets', txOctets = '$txOctets', timeCaptured = '$timeCaptured', periodicTime = '$periodicTime', datadatetime = '$datadatetime' where portid like '$portId'";
                    print 
    "New data updated - $sqlnewData \n\r \n\r";
                    
    $resnewData $connect->prepare($sqlnewData);
                    
    $resnewData->execute();
                
                    
    #Get port capacity
                    
    $sqlPortCapacity "select capacity from ports where portId like '$portId'";
                    
    $resPortCapacity $connect->prepare($sqlPortCapacity);
                    
    $resPortCapacity->execute();
                
                    while (@
    portcapacity $resPortCapacity->fetchrow_array()){
                        
    $capacity $portcapacity[0];
                    }
                
                    print 
    "Cap - $portId - $capacity \n\r \n\r";

                    
    #Check to see if port stats have been added to data tables
                    
    $sqlCheckPortOld "select * from data_old where portId = '$portId'"
                    
    $resCheckPortOld $connect->prepare($sqlCheckPortOld);
                    
    $resCheckPortOld->execute();
                    
    $numCheckPortOld $resCheckPortOld->rows;    
                    
                    
    $sqlCheckPortNew "select * from data_new where portId = '$portId'"
                    
    $resCheckPortNew $connect->prepare($sqlCheckPortNew);
                    
    $resCheckPortNew->execute();
                    
    $numCheckPortNew $resCheckPortNew->rows;
                    
                
                    if ((
    $numCheckPortOld 0) && ($numCheckPortNew 0)) {
                        
    $sqlOldPortStats "select rxOctets, txOctets, timeCaptured from data_old where portId like '$portId'";
                        
    $resOldPortStats $connect->prepare($sqlOldPortStats);
                        
    $resOldPortStats->execute();
                        
                        
                        
    $sqlPortStats "select rxOctets, txOctets, timeCaptured from data_new where portId like '$portId'";
                        
    $resPortStats $connect->prepare($sqlPortStats);
                        
    $resPortStats->execute();
                        
                        while (@
    oldportstats $resOldPortStats->fetchrow_array()){
                            
    $oldrxOctets $oldportstats[0];
                            
    $oldtxOctets $oldportstats[1];
                            
    $oldTime $oldportstats[2];
                        }
                        
                        while (@
    newportstats $resPortStats->fetchrow_array()){
                            
    $rxOctets $newportstats[0];
                            
    $txOctets $newportstats[1];
                            
    $time $newportstats[2];
                        }        
                        
                        
    $deltarx = ($rxOctets $oldrxOctets)*8;  #convert octets to bits
                        
    $deltatx = ($txOctets $oldtxOctets)*8;  #convert octets to bits
                        
    $deltaTime = ($time $oldTime) /1000#convert to seconds
                        
                        
    print     "Ports Stats - $rxOctets ($oldrxOctets) - $txOctets ($oldtxOctets)- $Time($oldTime)\n\r \n\r";
                        print     
    "Deltas - $deltarx -  $deltatx - $deltaTime \n\r\n\r";
                        
                        
    $txBits $deltatx/$deltaTime;
                        
    $rxBits $deltarx/$deltaTime;
                        
                        print  
    "\n\r\n\r";
                        print     
    "Rxbits/capacity ".$rxBits/$capacity." -  txbits/capacity"$txBits/$capacity." \n\r \n\r";
                            
                    
                        
    #Calculate percentage utilisation
                        #1 Gbit/sec = 1073741824 bits per sec
                        #2.5 Gbit (STM-16) = 2608857091 bits per sec
                        #10 Gbit/sec = 10737418240 bits per sec
                        
                        
    $rxUtil = ($rxBits/$capacity)*100;
                        
    $txUtil = ($txBits/$capacity)*100;
                        
                        print 
    "util - $rxUtil - $txUtil \n\r";
                        
                        
    #Check to see if this is an existing port in utilisation
                        
                        
    $sqlPortStats "select rxOctets, txOctets, timeCaptured from data_new where portId like '$portId'";
                        
    $resPortStats $connect->prepare($sqlPortStats);
                        
    $resPortStats->execute();
                        
                        
    $sqlCheckUtil "select * from utilisation where portId = '$portId'";
                        
    $resCheckUtil $connect->prepare($sqlCheckUtil);
                        
    $resCheckUtil->execute();
                        
    $numCheckUtil $resCheckUtil->rows;
                        
                        if (
    $numCheckUtil 0) {
                            
    #Update utilisation table    
                            
    $sqlUpdateUtil "update utilisation set rxutil = '$rxUtil', txutil = '$txUtil', datadatetime = '$datadatetime' where portId = '$portId'";
                            
    $resUpdateUtil $connect->prepare($sqlUpdateUtil);
                            
    $resUpdateUtil->execute();
                        } else {
                            
    $sqlInsertUtil "insert into utilisation (rxutil, txutil, portId, datadatetime) values ('$rxUtil', '$txUtil', '$portId', '$datadatetime')";
                            
    $resInsertUtil $connect->prepare($sqlInsertUtil);
                            
    $resInsertUtil->execute();
                        }
                    }    
                } else {
                    print 
    "No new data ....\n\r\n\r\n\r";
                }
                
            } else {

                
    #Insert new data into data_new    
                
    $sqlnewData "insert into data_new (rxOctets, txOctets, timeCaptured, periodicTime, portId, datadatetime) values ('$rxOctets', '$txOctets', '$timeCaptured', '$periodicTime', '$portId', '$datadatetime')";
                print 
    "New data Insert - $sqlnewData \n\r";
                
    $resnewData $connect->prepare($sqlnewData);
                
    $resnewData->execute();

            }
        }
    }
    close (MYFILE); 

    So I can see all of the status messages.

    This is B.pl

    PHP Code:
    $i 0;

    while (
    $i == 0) {
        print 
    "Extracting....\n\r";
        
    system(`perl extract.pl`);
         
        print 
    "Sleeping....\n\r \n\r";
        
    sleep 300;

    This is the output from B.pl

    PHP Code:
    C:\Abyss\htdocs\Dashboard\PostXML>perl dashboard.pl
    Extracting
    ....
    Sleeping.... 
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,873
    Rep Power
    1225
    Change this line:
    Code:
    system(`perl extract.pl`);
    to:
    Code:
    print `perl extract.pl`;

    Comments on this post

    • OmegaZero agrees : Or change the backticks to regular quotes
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Just a simple example.

    file a.pl:
    Perl Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
     
    print "A: $_ \n" for 1..5;
    print "A: system command \n";
    system "./b.pl 5";
    print "A: backticks command \n";
    print `./b.pl 10`;


    File b.pl
    Perl Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
     
    my $in = shift;
    print "B: $_ \n" for 1..$in;


    Execution of a.pl:
    Code:
    $ perl a.pl
    A: 1
    A: 2
    A: 3
    A: 4
    A: 5
    A: system command
    B: 1
    B: 2
    B: 3
    B: 4
    B: 5
    A: backticks command
    B: 1
    B: 2
    B: 3
    B: 4
    B: 5
    B: 6
    B: 7
    B: 8
    B: 9
    B: 10
    Just one clarification note on the backticks version. Even though b.pl is supposed to print 1 to 10, it does not do so to the screen when called with backticks, because this output is returned to the a.pl calling program. So it is actually the print in the a.pl program that prints the output supplied by the b.pl program. In other words, if the last line of the a.pl program were:

    Perl Code:
    `./b.pl 10`;


    the 1..10 numbers would not get printed: they have been returned to the a.pl program which just discards them.
    Last edited by Laurent_R; July 24th, 2013 at 04:58 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Barbados
    Posts
    147
    Rep Power
    59
    Originally Posted by FishMonger
    Change this line:
    Code:
    system(`perl extract.pl`);
    to:
    Code:
    print `perl extract.pl`;
    Perfect..that worked.


    Thanks much!!

IMN logo majestic logo threadwatch logo seochat tools logo