http://qs321.pair.com?node_id=1138544

vccs has asked for the wisdom of the Perl Monks concerning the following question:

I have a piece of code
#! /usr/local/bin/perl use Oraperl; use DBI; use FileHandle; use rf; use IO::Socket; use IO::Handle;; use POSIX qw(setsid); die "syntax: $0 port" if 1 > @ARGV; $port=$ARGV[0]; open STDIN, '>/dev/null' or die "Can't read /dev/null: $!"; print "Step 1\n"; open STDOUT, '>','/dev/null' or die "Can't write to $prout: $!"; print "Step 2\n"; open STDERR, '>/dev/null' or die "Can't write to $prerr: $!"; ...
This piece of code works fine on HP-UX with version:# perl -v This is perl, v5.8.3 built for IA64.ARCHREV_0-thread-multi-LP64 But now that we are migrating to Linux (RedHat 6.6), the code fails on the command: open STDOUT, '>','/dev/null' or die "Can't write to $prout: $!"; putting this line into comment, then it works. Perl version on LINUX: perl -v This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi Any ideas what is causing this?

Replies are listed 'Best First'.
Re: Perl migration hp-ux -> Linux ( perlport, File::Spec->devnull )
by Anonymous Monk on Aug 14, 2015 at 09:29 UTC
      I changed the code to
      use DBI; use FileHandle; use rf; use File::Spec; use IO::Socket; use POSIX qw(setsid); sub get_msg_id_and_type; sub REAP { 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; #$devnull = File::Spec->devnull(); open STDIN, '>/dev/null' || die "Can't read /dev/null: $!"; #open STDOUT, '>/dev/null' || die "Can't write to $prout: $!"; open STDOUT, File::Spec->devnull() || die "Can't write to $prout: $ +!"; open STDERR, '>>/dev/null' || die "Can't write to $prerr: $!"; defined(my $pid = fork) || die "Can't fork: $!"; exit if $pid;
      and it works. Is it because the /dev/null behaviour is different on HP-UX and LINUX?
        Another contributory factor might be that you are opening STDIN for output instead of input i.e. more normal would be:
        open STDIN, '/dev/null' || die "Can't read /dev/null: $!";

        One world, one people

Re: Perl migration hp-ux -> Linux
by pme (Monsignor) on Aug 14, 2015 at 09:12 UTC
    How does it fail? Could you copy/paste the error message?
      Well that's the issue, the perl program just stops there, no core file, nothing to go on. a sample program:
      #! /usr/local/bin/perl use Oraperl; use DBI; use FileHandle; use rf; use IO::Socket; use IO::Handle;; use POSIX qw(setsid); open STDIN, '>/dev/null' or die "Can't read /dev/null: $!"; print "Step 1\n"; #open STDOUT, '>','/dev/null' or die "Can't write to $prout: $!"; open(STDOUT, '>','/dev/null') or die "Can't write to $prout: $!"; print "Step 2\n"; open STDERR, '>/dev/null' or die "Can't write to $prerr: $!"; #SIG{CHLD} = 'IGNORE'; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; #$SIG{CHLD} = 'DEFAULT'; #--------------------------- # Get Environment Variables #--------------------------- $logdir = $ENV{out}; $constr = $ENV{CAAO_CSTR}; $user = $ENV{CAAO_USU}; $pass = $ENV{CAAO_PWD}; print"RUN\n";
      It only prints Step 1 But when the line is commented, it prints Step 1 Step 2 RUN

        If you reopen STDOUT to /dev/null, where do you expect the output of print to go?

        This line
        open(STDOUT, '>','/dev/null') or die "Can't write to $prout: $!";
        redirects STDOUT to /dev/null therefore all 'print' is redirected into /dev/null.

        If stepping through the program in Perl debugger doesn't help, try opening STDOUT/STDERR to some log file ("/tmp/$$.log", for example) or writing a __DIE__ signal handler:

        sub { return unless defined $^S; # bail out if it's a parser error open my $log, ">/tmp/crash@{[time]}_$$.log"; print $log @_; }