Perl wrapper to start daemon leaves zombie when run by cron
我有一个Perl脚本来作为守护进程启动进程。但是,当我从cron调用它时,我已经退出了进程。我已经将其简化为一个最小的脚本,我正在将\\'tail \\'作为守护程序的占位符开始:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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: $!\ "); my $logout ="logger -t test"; open (STDOUT,"|$logout") or die ("Cannot pipe stdout to $logout: $!\ "); open (STDERR,"|$logout") or die ("Cannot pipe stderr to $logout: $!\ "); my $cmd ="tail -f"; exec($cmd); exit(1); |
我用cron运行它,最后得到:
1 2 3 4 | 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)
谢谢,
leonstr
是造成它的管道。在" Programming Perl"中:
(在
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.