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

I'm trying to capture all output from a backup script I'm running as a cronjob. I save all of my backups in tar.7z files but for some reason only the output from 7z is being captured while the output from tar is not:

Specific code:

my $backupcommand = "tar -cvf - ".$directory."/".$seasonYear."*.html|7 +z a -mx9 -si ".$backupdirectory."/".$seasonYear.".tar.7z"; my $result = `$backupcommand 2>&1`;

The following is the resulting output in the terminal:

user@server:~$ perl dailyscript.pl > testout.txt tar: Removing leading `/' from member names /home/user/backupdirectory/2011.html user@server:~$

The following is the resulting output in testout.txt:

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_GB.UTF-8,Utf16=on,HugeFiles=on,4 CPUs) Creating archive /home/user/backupdirectory/2011.tar.7z Compressing [Content] Everything is Ok

I also tried the following and got the same result:

my $backupcommand = "tar -cvf - ".$directory."/".$seasonYear."*.html 2 +>&1|7z a -mx9 -si ".$backupdirectory."/".$seasonYear.".tar.7z"; my $result = `$backupcommand 2>&1`;

Any suggestions on how to capture all of it?

Replies are listed 'Best First'.
Re: Issue with capturing STDOUT using tar and p7zip
by Eliya (Vicar) on Mar 16, 2012 at 16:08 UTC

    Try running1 Run your $backupcommand in a subshell and redirect the output in its entirety:

    ... my $result = `( $backupcommand ) 2>&1`;

    P.S.: you can interpolate variables into a double-quoted string, which makes it somewhat easier to read:

    my $backupcommand = "tar -cvf - $directory/$seasonYear*.html | 7z a -m +x9 -si $backupdirectory/$seasonYear.tar.7z";

    ___

    1 upd: just tried it myself — works fine.

      Worked perfectly, thanks!
Re: Issue with capturing STDOUT using tar and p7zip
by glasswalk3r (Friar) on Mar 16, 2012 at 15:39 UTC

    Give a try to IPC-Cmd.

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill