如何重新初始化Perl的STDIN/STDOUT/STDERR? [英] How can I reinitialize Perl's STDIN/STDOUT/STDERR?

查看:124
本文介绍了如何重新初始化Perl的STDIN/STDOUT/STDERR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Perl脚本,它可以自动分叉并使其守护进程.它由cron运行,所以为了不让僵尸四处走动,我关闭了STDIN,STDOUT和STDERR:

open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
if (!fork()) {
  do_some_fork_stuff();
  }

我的问题是:在这一点之后,我想至少还原STDOUT(还原其他2个会很不错).但是,我需要像以前的STDOUT一样使用什么魔术符号重新打开STDOUT?

我知道,如果我从tty运行,则可以使用"/dev/tty"(但是我从cron运行,并取决于其他地方的stdout).我还读了一些技巧,您可以在其中将open SAVEOUT,">&STDOUT"放在一边,但是仅仅制作此副本的行为并不能解决原来留下僵尸的问题.

我正在查看是否有像open STDOUT,"|-"这样的魔术(我知道不是)以应有的方式打开STDOUT.

解决方案

如果仍然有用,请注意两点:

  1. 您可以仅在子进程中关闭STDOUT/STDERR/STDIN(即if(!fork()).这将允许父级继续使用它们,因为它们仍将在那里打开.) p>

  2. 我认为您可以使用更简单的close(STDOUT)而不是将其打开到/dev/null.

例如:

if (!fork()) {
    close(STDIN) or die "Can't close STDIN: $!\n";
    close(STDOUT) or die "Can't close STDOUT: $!\n";
    close(STDERR) or die "Can't close STDERR: $!\n";
    do_some_fork_stuff();
}

I have a Perl script which forks and daemonizes itself. It's run by cron, so in order to not leave a zombie around, I shut down STDIN,STDOUT, and STDERR:

open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
if (!fork()) {
  do_some_fork_stuff();
  }

The question I have is: I'd like to restore at least STDOUT after this point (it would be nice to restore the other 2). But what magic symbols do I need to use to re-open STDOUT as what STDOUT used to be?

I know that I could use "/dev/tty" if I was running from a tty (but I'm running from cron and depending on stdout elsewhere). I've also read tricks where you can put STDOUT aside with open SAVEOUT,">&STDOUT", but just the act of making this copy doesn't solve the original problem of leaving a zombie around.

I'm looking to see if there's some magic like open STDOUT,"|-" (which I know isn't it) to open STDOUT the way it's supposed to be opened.

解决方案

If it's still useful, two things come to mind:

  1. You can close STDOUT/STDERR/STDIN in just the child process (i.e. if (!fork()). This will allow the parent to still use them, because they'll still be open there.

  2. I think you can use the simpler close(STDOUT) instead of opening it to /dev/null.

For example:

if (!fork()) {
    close(STDIN) or die "Can't close STDIN: $!\n";
    close(STDOUT) or die "Can't close STDOUT: $!\n";
    close(STDERR) or die "Can't close STDERR: $!\n";
    do_some_fork_stuff();
}

这篇关于如何重新初始化Perl的STDIN/STDOUT/STDERR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆