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)

Elda Taluta; Sarks Sark; Ark Arks
My deviantART gallery


In reply to Forking and shelling out to curl by Argel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.