file redirection

P

paul_0403

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
 
J

Joost Diepenmaat

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;
 
T

Tim Greer

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.
 
J

John W. Krahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top