Perl wrapper to start daemon leaves zombie when run by cron

Posted by leonstr on Server Fault See other posts from Server Fault or by leonstr
Published on 2013-11-04T11:50:21Z Indexed on 2013/11/04 15:57 UTC
Read the original article Hit count: 391

Filed under:
|
|

I've got a Perl script to start a process as a daemon. But when I call it from cron I'm left with a defunct process. I've stripped this down to a minimal script, I'm starting 'tail' as a placeholder for the daemon:

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);

I run this with cron and end up with:

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

As far as I can tell it's the piping to logger that it doesn't like, if I send STDOUT and STDERR to /dev/null the problem doesn't occur.

Am I doing something wrong or is this just not possible? (CentOS 5.8)

Thanks,

leonstr

© Server Fault or respective owner

Related posts about centos

Related posts about bash