file redirection

Discussion in 'Perl Misc' started by paul_0403@yahoo.com, Oct 27, 2008.

  1. Guest

    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
     
    , Oct 27, 2008
    #1
    1. Advertising

  2. 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/
     
    Joost Diepenmaat, Oct 27, 2008
    #2
    1. Advertising

  3. Tim Greer Guest

    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!
     
    Tim Greer, Oct 27, 2008
    #3
  4. 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
     
    John W. Krahn, Oct 27, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    390
    Herbert Rosenau
    Aug 2, 2006
  2. TP
    Replies:
    2
    Views:
    323
  3. Rohit Srivastava

    File System Redirection Windows x 64 box.

    Rohit Srivastava, May 15, 2009, in forum: Python
    Replies:
    2
    Views:
    695
    Dave Angel
    May 15, 2009
  4. Gennady
    Replies:
    0
    Views:
    188
    Gennady
    Jun 28, 2003
  5. James T Kirk
    Replies:
    2
    Views:
    140
    James T Kirk
    Aug 21, 2003
Loading...

Share This Page