-1

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?

PS: I'm using a Mac OS X now, but this will run on Linux.

Chrome
  • 1
  • 1
  • http://superuser.com/q/276909/416314 – serenesat Nov 19 '15 at 05:21
  • 1
    You need to either ignore SIGCHLD signals (so your child processes won't become zombies), or you need to use `waitpid()` or equivalent with `WNOHANG` option to clean up any children that have completed. Or, possibly, you need to have the child fork before executing, and the parent waits for the child to die, while the grandchild is inherited by `init` process which mainly sits around waiting for other processes' children to die. Somehow, you need to make sure your dead are properly buried; things get messy if you don't, and the system won't let play unless you clean up your mess. – Jonathan Leffler Nov 19 '15 at 06:17

1 Answers1

0

Your problem is this:

while () {

doesn't have any constraint condition, so it's just spinning as fast as it can. You're never actually reading from your pipe, you're just forking as fast as you can and spawning that new script.

You might be wanting:

while ( my $line =  <$pipe> ) { 

    #....
}

But really - it's arguable that you don't actually need to fork at all, because a read/process/read loop would probably do just fine - fork() and exec() is basically what system already does anyway.

You should also - if forking - clean up child processes. It doesn't matter too much for short running things, but things that sit in a loop will leave a lot of zombie processes. Either via setting $SIG{CHLD} or using waitpid.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • You're right, indeed. Actually, I forgot to write the condition in the post.. The real code has a condition: while (<$pipe>) I'll fix the original post, sorry.. I used fork to keep the execution of the main program (prgTAIL) without waiting for the slave program (prgPROCESS) to end. I think the problem is about zombie process. I'll test the code with $SIG{CHLD} or something so "my dead will be properly buried" and I let you know. Thanks! – Chrome Nov 23 '15 at 23:55