LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-25-2009, 11:16 AM   #1
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Rep: Reputation: 30
Perl stderr&stdout redirect question


Hi,

I am trying to do the following in a perl script,

my $disk="hdc";
system("dd if=/dev/sdb of=/dev/${disk} &> /tmp/${disk}.txt &");

Basically I want to start a dd, output stdout and stderr to a file and fork it.

I have another bash script which does a "kill -USR1 `pidof dd`" in the background which runs once a second. Sending USR1 as you might know causes dd to send the current status out to stderr.

It all works apart from that the file /tmp/hdc.txt becomes blank if I run it form within perl. If I just type "dd if=/dev/sdb of=/dev/hdc &> /tmp/hdc.txt &" in a bash prompt then it does work exactly as I want it to.

Why is this? What am I doing wrong? I can only assume that it is perl who steals all the stdout and wont let me redirect it to a file?


Thanks!
 
Old 03-25-2009, 11:30 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
/bin/sh redirection of both stdout and stderr is:

Code:
program 1>screen.log 2>&1
And that's what you need.
 
Old 03-25-2009, 11:39 AM   #3
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Sergei Steshenko View Post
/bin/sh redirection of both stdout and stderr is:

Code:
program 1>screen.log 2>&1
And that's what you need.
Thank you, I tried this but still the same problem. It did however work in the shell, but not in Perl.
 
Old 03-25-2009, 11:51 AM   #4
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Fredde87 View Post
Thank you, I tried this but still the same problem. It did however work in the shell, but not in Perl.
Okay ignore everything in this thread. The reason doesnt seem to be due to Perl. I should have mentioned that this script is a CGI script. If I run the script in a shell it does actually work as well.

I am trying to run the script on a lighttpd server which is running as root (I know, bad but it is a internal network server which is just going to be used to mirror hard drives).

I can see dd in the process list so it does appear to start and the hard disk activity light is flashing so it appears to be copying fine as well. It is just that I cant seem to grab the stderr. What could be causing this?

Last edited by Fredde87; 03-25-2009 at 11:53 AM.
 
Old 03-25-2009, 01:23 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Fredde87 View Post
What could be causing this?
Try this
Code:
my $disk="hdc";
system("dd if=/dev/sdb of=/dev/${disk} > /tmp/${disk}.txt 2>&1 &");
The first ">" tells the shell to redirect stdout to the file. The final "2>&1" tells the shell to redirect stderr to stdout. That has to be last. I forget why.

This webpage has some interesting information on the importance of the order of redirection.

Anyway, it worked here for this example:
Code:
#!/usr/bin/perl -w
my $disk="hdc";
system("dd if=/dev/zero of=${disk}.bin > ${disk}.txt 2>&1 &");

Last edited by David1357; 03-25-2009 at 01:28 PM. Reason: Added link to page with information on importance of order of redirection
 
Old 03-26-2009, 04:43 AM   #6
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by David1357 View Post
Try this
Code:
my $disk="hdc";
system("dd if=/dev/sdb of=/dev/${disk} > /tmp/${disk}.txt 2>&1 &");
The first ">" tells the shell to redirect stdout to the file. The final "2>&1" tells the shell to redirect stderr to stdout. That has to be last. I forget why.

This webpage has some interesting information on the importance of the order of redirection.

Anyway, it worked here for this example:
Code:
#!/usr/bin/perl -w
my $disk="hdc";
system("dd if=/dev/zero of=${disk}.bin > ${disk}.txt 2>&1 &");
Sorry, that didnt work either. But I dont think the redirect is the problem anymore. After some googling I have found out that most webservers wont allow stderr to be redirected. It seems that they will take any stderr output and put it to their error.log file.

This seems to be the case as I can see a lot of stderr output in it. However not the one I am trying to redirect.

I have read a bit about people suggesting to use Tie::STDERR or CGI::Carp but I am unsure how to do this.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is wrong with this perl code ? STDOUT / STDERR issue ? rtspitz Programming 4 10-08-2007 06:53 PM
shell script: how to send stdout one place & stderr another? BrianK Programming 8 09-21-2007 06:57 AM
Shell script - stdout & stderr to terminal and file jantman Linux - Software 1 12-07-2006 04:34 PM
shell short question(stdout, stderr) blackzone Programming 4 11-11-2004 09:01 AM
TCSH: redirect stdout and stderr seperately ugenn Linux - General 2 06-06-2002 12:07 PM


All times are GMT -5. The time now is 02:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration