chrome has asked for the wisdom of the Perl Monks concerning the following question:

Hi superMonks,

I'm trying to write a program, actually a daemon, which stay in memory and perform something like tail -F on a rapidly updated log file. Then the program, when detect a new line on the file, have to launch another compiled perl script which will perform some operations on the log line and then send it with a post.

To clearly explain, I will refer to these two program as "prgTAIL" and "prgPROCESS". So, prgTAIL tail the log and launch prgPROCESS passing the new line to it.

Obviously the prgTAIL doesn't have to wait for the prgPROCESS to end the process, cause prgTAIL have to stay in memory and keep detecting new line on the log. Also, the rate of file update needs to launch multiple parallel prgPROCESS instance. For this reason I'm using two program: the first small and fast just pass the data to the second, which may be heavier cause it can be launched in multiple instances.

On the prgTAIL I used:

- a pipe to tail the log file

- a while loop to launch prgPROCESS on new log line

- a fork(); to continue without waiting prgPROCESS ends

my $log_csv = "/log/csv.csv"; open (my $pipe, "-|", "tail", "-n0", "-F", $log_csv) or die "error"; while (<$pipe>) { $line = $_ ; my $pid = fork(); if (defined $pid && $pid == 0) { exec("/bin/prgPROCESS ".$line) ; # I tried system() too. exit 0; } }

The prgPROCESS operation are not so important; anyway.. it parses the $line passed as arguments, construct an XML and then post it via https.

So, this stuff actually run, but I think I messed up something with the process, cause when a reach a number of newline and prgPROCESS call around 550, prgTAIL keep running but it can't call prgPROCESS anymore, cause there are too many process. I get this error on the bash:

 -bash: fork: Resource temporarily unavailable

What's wrong? Any idea? Maybe the prgPROCESS processes don't end and stay stuck without make room for other process?

Replies are listed 'Best First'.
Re: A daemon to tail a log and fork multiple external (perl) script
by BrowserUk (Patriarch) on Nov 19, 2015 at 05:19 UTC
    Maybe the prgPROCESS processes don't end and stay stuck without make room for other process?

    Yes. You are creating zombie processes and not collecting them.

    Adding:

    $SIG{CHLD} = 'IGNORE';

    to the top of your program will likely make the problem go away.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: A daemon to tail a log and fork multiple external (perl) script
by afoken (Chancellor) on Nov 19, 2015 at 18:50 UTC
     exec("/bin/prgPROCESS ".$line) ;

    Just two little notes:

    • exec may fail. You should handle that situation. RTFM for details.
    • Pasting all of the arguments into a string, unquoted, begs for trouble. Imaginge $line containing the string " & ; rm -rf /". Quoting is not an option, because shells tend to have different quoting rules. Use the multi-argument form of exec to avoid the shell completely.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)