我尝试做的是打开外部进程的写管道的文件句柄,然后将STDERR重定向到文件句柄.这是我的测试脚本,遗憾的是还没有用.
#!/usr/bin/perluse strict;use warnings;# open write filehandle to external processopen my $fh,'|-','pipefile_http',or dIE "Couldn't open logfile: $!\n";# redirect STDERR from parent process to same write filehandle to child processmy $fileno = fileno($fh);open STDERR,">&$fileno" or dIE "Couldn't switch STDERR to fileno $fileno: $!\n";print $fh "1. print to file handle\n";print STDERR "2. print to STDERR\n";print "3. print to STDOUT\n";close $fh;exit 0;
当我运行此脚本时,它成功地将对STDERR的打印调用重定向到外部日志记录过程,但是对$fh的打印调用不起作用(消息消失).此外,脚本在成功将消息#3打印到STDOUT后无限期挂起.当我用strace运行脚本时,我可以看到脚本挂在waitpID()调用(外部进程的pID)上.
有关如何做到这一点的任何建议?
解决方法 只需重新分配STDERR:#!/usr/bin/perluse strict;use warnings;# open write filehandle to external processopen my $fh,or dIE "Couldn't open logfile: $!\n";# reassign STDERR*STDERR = $fh;print $fh "1. print to file handle\n";print STDERR "2. print to STDERR\n";print "3. print to STDOUT\n";close $fh;exit 0;总结
以上是内存溢出为你收集整理的perl – 将父进程的STDERR重定向到子进程的文件句柄全部内容,希望文章能够帮你解决perl – 将父进程的STDERR重定向到子进程的文件句柄所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)