I am trying to capture output that I need, thus creating my own logfile after script execution. I am using the system() command to execute (2) programs.
The first one is calling another perl script, the second one is a simple printout of the environment, using 'env', but I want it to be appended to the logfile I am creating rather than going to standard out.
Here is my working code:
#!/usr/local/bin/perl -w
use warnings;
use strict;
#Call the main perl script
my $runrc = "./getinfo.pl";
my $chkenv = "/bin/env";
#Run the command
open (MYLOG, ">>/home/mylog") || die "cannot append $!";
my $rc = system ($runrc); #runs the command
if ($rc == 0)
{
print MYLOG "system call worked \n";
}
elsif ($rc == -1)
{
print MYLOG "could not even start the program.:$!\n";
}
else
{
my $exit_signal = $rc & 0x7f;
my $has_coredump = $rc & 0x80;
my $exit_value = $rc >> 8;
}
my $rc2 = system ($chkenv); #runs the command
if ($rc2 == 0)
{
print MYLOG "system call ENV worked: \n";
print MYLOG "$rc2\n";
}
elsif ($rc2 == -1)
{
print MYLOG "could not even start the program.:$!\n";
}
else
{
my $exit_signal = $rc2 & 0x7f;
my $has_coredump = $rc2 & 0x80;
my $exit_value = $rc2 >> 8;
}
close (MYLOG);
The first one is calling another perl script, the second one is a simple printout of the environment, using 'env', but I want it to be appended to the logfile I am creating rather than going to standard out.
Here is my working code:
#!/usr/local/bin/perl -w
use warnings;
use strict;
#Call the main perl script
my $runrc = "./getinfo.pl";
my $chkenv = "/bin/env";
#Run the command
open (MYLOG, ">>/home/mylog") || die "cannot append $!";
my $rc = system ($runrc); #runs the command
if ($rc == 0)
{
print MYLOG "system call worked \n";
}
elsif ($rc == -1)
{
print MYLOG "could not even start the program.:$!\n";
}
else
{
my $exit_signal = $rc & 0x7f;
my $has_coredump = $rc & 0x80;
my $exit_value = $rc >> 8;
}
my $rc2 = system ($chkenv); #runs the command
if ($rc2 == 0)
{
print MYLOG "system call ENV worked: \n";
print MYLOG "$rc2\n";
}
elsif ($rc2 == -1)
{
print MYLOG "could not even start the program.:$!\n";
}
else
{
my $exit_signal = $rc2 & 0x7f;
my $has_coredump = $rc2 & 0x80;
my $exit_value = $rc2 >> 8;
}
close (MYLOG);