Hello fellow Monks! I'm trying to write a daemon that runs a curl command.
use IO::Socket;
use POSIX qw(WNOHANG setsid);
sub daemonize {
$SIG{CHLD} = 'IGNORE'; # Configure to autoreap zombies
die "Can't fork" unless defined ( my $child = fork ); # FORK
+<<<<<<<<<<<<
CORE::exit(0) if $child; # Parent exits
setsid(); # Become session leader
open( STDIN, "</dev/null" ); # Detach STDIN from shell
open( STDOUT, ">/dev/null" ); # Detach STDOUT from shell
open( STDERR, ">&STDOUT" ); # Detach STDERR from shell
chdir '/tmp'; # Change working directory
umask(0); # Reset umask
$ENV{PATH} = '/bin:/sbin:/usr/sbin'; # Reset PATH
}
Looks like my script is hanging when I daemonize it. I'm logging to a CSV file, and I finally added some code in tracing this back to where it is hanging, and it looks like it's when I shell out.
sub cmd_wrapper {
my $cmd = shift;
my @out;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 15;
## (CSV output logged) ##
@out = qx[$cmd]; #<<<<< Shell out
## (CSV output *NOT* logged) ##
alarm 0;
};
return $@ if $@ ne ""
return wantarray ? @out : join '', @out;
}
What's even stranger is if I uncomment my $? == -1 check in the subroutine that calls cmd_wrapper I see log entries indicating it failed (though I appear to be getting valid results back; guessing $? doesn't work well when forking). But I'm at loss to explain how code appearing after I shell out affects the shelling out behavior.
## Run the Curl command ##
my $restURL = $opt_ref->{restURL} . $opt_ref->{pagingURL};
say "DEBUG: \$restURL: \]$restURL\[" if $cfg{debug} or $cfg{test};
my $cmd = "$curlCommand -X $opt_ref->{method} $restURL";
my $results = cmd_wrapper( $cmd, $opt_ref, $info_ref, $cfh{csv_exp
+ort}, \@rpt_hdr, 1 );
say 'DEBUG: $results: ', Dumper $results if $cfg{debug}; #or $cfg{
+test} > 1;
$info_ref->{name} = $?;
$info_ref->{primary} = $cmd;
$info_ref->{status_msg} = $results;
#=pod
# Uncommented I see results, Commented out cmd_wrapper seems to hang.
## CASE: Connection issue
if( $? == -1 ) { # WARN: Will this still work now that I'm call
+ing cmd_wrapper()???
$info_ref->{entry_level} = 'DEBUG';
$info_ref->{status} = 'runCurl connection failed';
csv_log( $opt_ref, $info_ref, $cfh{csv_export}, \@rpt_hdr, 1 )
+;
return; #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+RETURN
}
#=cut
Am I missing something simple? Something with buffering? Or hitting an optimization problem? Something to do with that return? Or....?
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-li
+nux
Red Hat Enterprise Linux Server release 5.11 (Tikanga)