centos - 用于启动守护进程的 Perl 包装器在由 cron 运行时会留下僵尸

标签 centos bash perl

我有一个 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/

相关文章:

windows - Ghostscript On Windows 导致 -dPDFSETTINGS 选项无效

bash - 哪个 Unix 命令可以从这个字符串中获取数字?

linux - 为位置参数声明 -n

perl - 使用逻辑定义的运算符将数组映射到散列

java - 寻找一种更快的方法来执行字符串搜索

linux - 如何编写脚本以使用我的凭据连接到 ssh 服务器?

php - yum 在 centos 上安装 php-pear*

centos - 在 Asterisk 中生成非十进制唯一 ID

mysql - 使用 ndoutils-2.0.0 将数据从 Nagios 存储到 MariaDB

mysql - 如何在 CentOS 中加密现有的 mariadb 表?