我有一个 Perl 脚本来启动一个进程作为守护进程。但是当我从 cron 调用它时,我留下了一个已失效的进程。我已经把它简化为一个最小的脚本,我开始'tail'作为守护进程的占位符:
use POSIX "setsid";
$SIG{CHLD} = 'IGNORE';
my $pid = fork();
exit(0) if ($pid > 0);
(setsid() != -1) || die "Can't start a new session: $!";
open (STDIN, '/dev/null') or die ("Cannot read /dev/null: $!\n");
my $logout = "logger -t test";
open (STDOUT, "|$logout")
or die ("Cannot pipe stdout to $logout: $!\n");
open (STDERR, "|$logout")
or die ("Cannot pipe stderr to $logout: $!\n");
my $cmd = "tail -f";
exec($cmd);
exit(1);
我用 cron 运行它并最终得到:
root 18616 18615 0 11:40 ? 00:00:00 [test.pl] <defunct>
root 18617 1 0 11:40 ? 00:00:00 tail -f
root 18618 18617 0 11:40 ? 00:00:00 logger -t test
root 18619 18617 0 11:40 ? 00:00:00 logger -t test
据我所知,它不喜欢记录器的管道,如果我将 STDOUT 和 STDERR 发送到/dev/null 问题就不会发生。
我做错了什么还是这不可能? (CentOS 5.8)
谢谢,
莱昂斯特
最佳答案
是管道造成的。来自“编程 Perl”:
(在 fork
上):
If a forked child inherits system file descriptors like STDIN and STDOUT that are connected to a remote pipe or socket, you may have to reopen these in the child to /dev/null. That’s because even when the parent process exits, the child will live on with its copies of those filehandles.
关于centos - 用于启动守护进程的 Perl 包装器在由 cron 运行时会留下僵尸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796919/