CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
redirect stderr to stdout hungs

 



dprabhu
New User

Feb 10, 2017, 1:21 AM

Post #1 of 5 (1374 views)
redirect stderr to stdout hungs Can't Post

Hello,

I've a perl script for installation of our product which was working good with no issues. Recently using the script hungs at a point and I could see that following code hungs

Code
sub exec_local_cmd 
{
my($cmd,$self) = @_;
my $log = $self->get_logger();
my $status = $NOT_OK;
my $err_str;
my $redirect = "2>&1";

$log->info("COMMAND EXECUTED:\n $cmd $redirect");
my $output = `$cmd $redirect`;
}

If I remove the $redirect from last line program continues to work.
I was trying to run linux service using the command.

Please suggest.

Thanks.


(This post was edited by FishMonger on Feb 10, 2017, 7:39 AM)


BillKSmith
Veteran

Feb 10, 2017, 6:07 AM

Post #2 of 5 (1369 views)
Re: [dprabhu] redirect stderr to stdout hungs [In reply to] Can't Post

This appears to be more of a Linux problem than a perl problem. We must know the exact command that you are trying to execute. If your info method updates a log file every time it is called, the last entry in the log is probably the offending command. Please post that entry. If you are lucky, we will see the problem. If not, you will have to add debugging statements to verify that we are working on the right command.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Feb 10, 2017, 7:38 AM

Post #3 of 5 (1363 views)
Re: [dprabhu] redirect stderr to stdout hungs [In reply to] Can't Post

The most likely reason it's hanging is because it's waiting for user input.

Using backticks to run commands is the easiest and shortest method, but far from the best. In cases like this you probably should be using IPC::Open3 or IPC::Run so that you can properly handle the STDIN, STDOUT and STDERR of the command.


FishMonger
Veteran / Moderator

Feb 10, 2017, 7:47 AM

Post #4 of 5 (1361 views)
Re: [dprabhu] redirect stderr to stdout hungs [In reply to] Can't Post

You should adjust your log entries a little so that they are more accurate.

In this case you're logging "COMMAND EXECUTED:" prior to actually executing the command. Instead that entry should say "EXECUTING COMMAND" and then after the command finishes, you should log the success or failure status along with an error message if it failed in some way.


dprabhu
New User

Feb 12, 2017, 8:06 PM

Post #5 of 5 (1343 views)
Re: [dprabhu] redirect stderr to stdout hungs [In reply to] Can't Post

My complete pgm is as below --

Code
sub exec_local_cmd  
{
my($cmd,$self) = @_;
my $log = $self->get_logger();
my $status = $NOT_OK;
my $err_str;
my $redirect = "2>&1";

$log->info("COMMAND EXECUTED:\n $cmd $redirect");
my $output = `$cmd $redirect`;
if ( $? == 0 )
{
$status = $OK;
if(defined($output) && $output ne "" )
{
$log->info("COMMAND OUTPUT:\n $output");
}
}
else
{
if(defined($output) && $output ne "" )
{
$log->error($output);
}
}
return ($status,$output);
}

COMMAND EXECUTED : service mysql start 2>&1

In Reply To


(This post was edited by FishMonger on Feb 13, 2017, 9:34 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives