Question

I am creating Perl threads in a "master" script that call a "slave" perl script through system calls. If this is bad, feel free to enlighten me. Sometimes the slave script being called will fail and die. How can I know this in the master script so that I can kill the master?

Is there a way I can return a message to the master thread that will indicate the slave completed properly? I understand it is not good practice to use exit in a thread though. Please help.

================================================================================== Edit:

For clarification, I have about 8 threads that each run once. There are dependencies between them, so I have barriers that prevent certain threads from running before the initial threads are complete.

Also the system calls are done with tee, so that might be part of the reason the return value is difficult to get at.
system("((" . $cmd . " 2>&1 1>&3 | tee -a $error_log) 3>&1) > $log; echo done | tee -a $log"

No correct solution

OTHER TIPS

The way you have described your problem, I don't think using threads is the way to go. I would be more inclined to fork. Calling 'system' is going to fork anyway.

use POSIX ":sys_wait_h";

my $childPid = fork();
if (! $childPid) {
    # This is executed in the parent
    # use exec rather than system, so that the child process is replaced, rather than
    # forking a new subprocess (or maybe even shell) to run your child process
    exec("/my/child/script") or die "Failed to run child script: $!";
}

# Code here is executed in the parent process
# you can find out what happened to the parent process by calling wait
# or waitpid. If you want to be able to continue processing in the
# parent process then call waitpid with second argument WNOHANG

# EG. inside some event loop, do this
if (waitpid($childPid, WNOHANG)) {

    # $? now contains the exit status of child process
    warn "Child had a problem: $?" if $?;

}

There is probably CPAN module that is well suited for what you are trying do. Maybe Proc::Daemon - Run Perl program(s) as a daemon process..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top