Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > file redirection

Reply
Thread Tools

file redirection

 
 
paul_0403@yahoo.com
Guest
Posts: n/a
 
      10-27-2008
I have a perl program that opens a log file and than prints to it,
that part works well

ie

if (! open (LOG, ">>$logfile"))
{
print STDERR "Cannot open logfile $logfile $!\n";
exit 1;
}

print LOG "Hello world\n";


I want to do something like this and it is not working. Note I want
to redirect the output of the kill command to the same logfile, is
this
possible? If so, what am I missing. Note: at this point LOG is still
open.

system("kill -USR1 \"$1\" > LOG 2>\&1");

Thanks


 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      10-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> I have a perl program that opens a log file and than prints to it,
> that part works well
>
> ie
>
> if (! open (LOG, ">>$logfile"))
> {
> print STDERR "Cannot open logfile $logfile $!\n";
> exit 1;
> }
>
> print LOG "Hello world\n";
>
>
> I want to do something like this and it is not working. Note I want
> to redirect the output of the kill command to the same logfile, is
> this
> possible? If so, what am I missing. Note: at this point LOG is still
> open.
>
> system("kill -USR1 \"$1\" > LOG 2>\&1");


LOG is not available as a filehandle name in any exec'd programs, and
you have to use numeric arguments in the shell (which you can get from
fileno) to denote file descriptors.

The only file descriptors that are passed on to child processes by
default are STDOUT, STDIN and STDERR. This is handled using the
FD_CLOEXEC (close-on-exec) flag. See the POSIX standard, or a decent
book on UNIX programming.

So, you basically have 2 options: remove the close-on-exec flag on LOG
(see the code below), or redirect STDERR to some log file and then
exec. The latter is simpler and usually what you want anyway for perl
code, since it means your die()/warn() messages go to that log.

#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);

open LOG,">log.log" or die $!;

# remove the close-on-exec flag from LOG
fcntl(LOG, F_SETFD, fcntl(LOG, F_GETFD, 0) & (-1 ^ FD_CLOEXEC));

my $fn = fileno(LOG);
system qq(echo "test" >&$fn 2>&$fn) and die;

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      10-27-2008
(E-Mail Removed) wrote:

> system("kill -USR1 \"$1\" > LOG 2>\&1");


system doesn't know what the LOG filehandler is there.

Since you're doing it this way, perhaps just only use:

system("kill -USR1 \"$1\" > $logfile 2>\&1");

Instead of using open() and then system()?

Also, you probably want >> instead of > to append to the file, rather
than overwrite it by the sound of it.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-27-2008
(E-Mail Removed) wrote:
> I have a perl program that opens a log file and than prints to it,
> that part works well
>
> ie
>
> if (! open (LOG, ">>$logfile"))
> {
> print STDERR "Cannot open logfile $logfile $!\n";
> exit 1;
> }
>
> print LOG "Hello world\n";
>
>
> I want to do something like this and it is not working. Note I want
> to redirect the output of the kill command to the same logfile, is
> this possible? If so, what am I missing.


perldoc -f kill

> Note: at this point LOG is still open.
>
> system("kill -USR1 \"$1\" > LOG 2>\&1");




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File System Redirection Windows x 64 box. Rohit Srivastava Python 2 05-15-2009 01:02 PM
redirection in a file with os.system TP Python 2 11-04-2008 05:58 AM
detecting EOF using cin with file redirection iaesun@yahoo.com C Programming 7 08-02-2006 05:17 AM
File Download and page redirection from one click James T Kirk Javascript 2 08-21-2003 01:36 PM
exit! affects stdout redirection to a file Gennady Ruby 0 06-27-2003 11:41 PM



Advertisments