Redirect system output to null in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect system output to null in perl
# 1  
Old 08-25-2011
Redirect system output to null in perl

Hi Guys,

Please help me.. it is urgent. I am writing a perl script to capture command output and redirect it to a logfile.At the same i want to check the return code of the command and log it if the command is not succesful in my logfile.. Here is my code, it is working but system command inside FORK is printing output to stdout, i don't want it. Instead i just want to see the exit status of the command and it's output to a file:
please help:
Code:
sub my_fork {
  my $cmd = $_[0];
  my $pid;

 FORK: {
    if ($pid = fork) {
    return $pid;
    } elsif (defined $pid) {
    log_message("------------$cmd output BEGIN-------------");
    my $ret = system($cmd) ;
        if($ret !=0){
            print "$cmd not successful\n";
            log_and_exit("$cmd not successful",16) ;
    }
        else{
            my @output = `$cmd`;
            log_message(@output);
    #       log_message("------------$cmd output END---------------");
            exit 0;
        }
    } elsif ($! =~ /No more process/) {
    # EAGAIN, supposedly recoverable fork error
    sleep 5;
    redo FORK;
    } else {
    # weird fork error
    log_and_exit("Can't fork: $!\n",$failure);
    }
  }
}

sub execute_command_rc {
    my $expectedrc = $_[0];
    my $command = $_[1];
    my $pid = my_fork($command);
    my $returned_pid = waitpid($pid,0);
    my $status = $?;
    log_and_exit("no child procs to collect?????",$failure) if ($returned_pid == -1);
    log_and_exit("returned pid: $returned_pid should have been $pid",$failure)
    if ($returned_pid != $pid);
    my $exit_value = $status >> 8;
    my $signal_num = $status & 127;
    my $dumped_core = $status & 128;

    log_message("$command exited with status: 16 (instead of $expectedrc)")
    if ($exit_value != $expectedrc);

    log_message("$command killed by signal: $signal_num")
    if ($signal_num);

    log_message("$command dumped core")
    if ($dumped_core);

    log_message("------------$command output END---------------");
    # emulate bash behavior. if process is killed, then return (128 | signal no.)
    return ($signal_num | 128) if ($signal_num);
    return $exit_value;
}
execute_command_rc(0,'ps -eo vsz,pid,args | sort -n');

Moderator's Comments:
Mod Comment Please use [CODE] tags when posting source listings, etc. It makes for an easier read

Last edited by pludi; 08-25-2011 at 12:31 PM..
# 2  
Old 08-26-2011
Code:
my @output = `$cmd` ; 
if(($?>>8) !=0){ # see perlvar manpage (CHILD_ERROR) 
    print "$cmd not successful\n"; 
    log_and_exit("$cmd not successful",16) ;     
} else { 
    log_message(@output);     
    #       log_message("------------$cmd output END---------------");             
    exit 0;         
}

# 3  
Old 08-29-2011
Thanks hfreyer

Thanks dude it's working for me... Smilie

Cheers,
Sriram
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

2. UNIX for Dummies Questions & Answers

Redirect Standard Error to /dev/null is not working.

Hello. When I run a .ksh that contains the command below, and there is no file available in the source location the "FILE_NAME_*.CSV not found" error is still being displayed. FILEN=$(ssh ${SOURCE_SERV} "cd ${SOURCE_LOCATION} ;ls ${FILES}") 2> /dev/null. This is interfering with the rest... (4 Replies)
Discussion started by: jimbojames
4 Replies

3. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

4. Shell Programming and Scripting

redirect the audio output to /dev/null

I'm using an text-to-speech synthesis in a script, and I need to redirect it's output to /dev/null how can I do that ? And how to redirect the stream to his normal output then (sound card ) ? thankx (2 Replies)
Discussion started by: firelink
2 Replies

5. Red Hat

perl backticks: can't redirect output.

Hi everyone. This is a bit of a perl/linux mixed question. I am trying to redirect STDOUT of chsh by using the following line of perl code. system ("chsh -s /sbin/nologin $testing 1>/dev/null"); This should redirect STDOUT to /dev/null but it won't do that for some odd reason. Any ideas or... (6 Replies)
Discussion started by: austinharris43
6 Replies

6. Shell Programming and Scripting

perl: looping through the output of a 'system' command

Hi there could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches? for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or... (3 Replies)
Discussion started by: rethink
3 Replies

7. Shell Programming and Scripting

How to redirect the import log to /dev/null?

Hi i am running oracle database import through a script and script scans the import log to see if there are any errors. Now the porblem is that when i run the script the import log appears on the screen even if i direct the output of import to /dev/null. imp "'/ as sysdba'"... (5 Replies)
Discussion started by: vinoo128
5 Replies

8. Shell Programming and Scripting

perl redirect output to file ..not working

here is simple perl script i wanted for my net connection ... just to check if default gateway is pingable or not if not write in log file but problem is that i can not write in file i can print on STDOUT but not in file ...why so ?? same thing was there when i was tying to write on sockets... (7 Replies)
Discussion started by: zedex
7 Replies

9. UNIX for Dummies Questions & Answers

redirect stderr and/or stdout to /dev/null from command line

Is it possible to redirect errors at the command line when you run the script such as bash scriptname & 2>/dev/null? (1 Reply)
Discussion started by: knc9233
1 Replies

10. Shell Programming and Scripting

redirect stderr to dev/null in bash env.

Working in a bash environment, in the following example, how do I direct the error message that putting in an invalid flag (-j for example) would normally produce to dev/null? while getopts "abcd" opt do case "$opt" in i) a etc ;; r) b etc ;; f) c etc ;; v) d... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question