开发者

Perl Script, Fork/Exec, System claims my process has died when in fact only my child process has died

开发者 https://www.devze.com 2022-12-12 06:00 出处:网络
I have a Perl script that does a fork/exec to start another tool in the background and monitor some file system changes while this other tool is running. This seems to work like expected.

I have a Perl script that does a fork/exec to start another tool in the background and monitor some file system changes while this other tool is running. This seems to work like expected.

When I start this Perl script from a shell (e.g. Bash), of course the shell prompt should be gone for as long as my Perl script is running. And it will keep running until the expected file modification has taken place; but there is no guarantee that the file modification might be done by the external tool, in that case the external tool will exit, but my script will keep running and has to handle that situation somehow - this handling is beyond the scope of the question and not related to my problem (so far it is not even implemented).

My prob开发者_如何学Clem is that as soon as my child process dies, Bash returns to its prompt, claiming my process has finished running... which is not true. It clearly is still running in the background and it still waits for the file system modification. If I keep printing some text in the main loop of the script, this text is still printed, even though bash has returned back to the prompt already.

I cannot figure out what makes bash believe my process has quit. I tried blocking the SIGCHLD signal in my script, I tried closing and/or redirecting STDOUT/STDERR/STDIN (which are duplicated on fork, but you never know) - no success. I even tried the famous "double fork", to make the final child independent of my script process, same outcome. No matter what I do, as soon as my child (or grandchild) dies, Bash beliefs my process has quit. Starting my script in the background (using "&" at the end) makes Bash even tell me that process XYZ has finished (and it names my process here, not the child process, even though my process is happily alive and printing to terminal via STDOUT that very moment).

If this was only an issue of Bash, I couldn't care any less, but other third party software that is supposed to run my script acts the same way. As soon as my child dies, they claim that my script has in fact died, which is simply not true.


Just a sanity check, is your main program walking the right fork? It should follow the non-zero path:

my $pid = fork;
if ($pid == 0) {
    print "Child\n";
} else {
    print "Main\n";
}

From man fork:

Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.

0

精彩评论

暂无评论...
验证码 换一张
取 消