我所做的:
伪代码:
$SIG{CHLD} = ‘IGNORE’; # or waitpid($pid,0) in the parent process
$pid = fork();
if($pid == 0)
{
close STDOUT; # So that the parent sends the response to the client right away.
@errorMsgs = qx(tar up big directories over 50G…); # This can go on for a few minutes.
if($? ==0) { Send a ‘success’ email } # Is always false ($? == -1)
else { Send a ‘failure’ email }
}
elsif($pid){ sendResponse; waitpid($pid,0) if $SIG{CHLD} != 'IGNORE'; exit;}
我的问题:
无法从 qx() 获取正确的返回代码 ($?) 和任何错误消息,因为 ($SIG{CHLD} = 'IGNORE') 将其设置为 -1。如果我删除 $SIG{CHLD} 语句,客户端网页不会收到来自父级的响应消息,直到子级被收割。
最佳答案
你得到 -1 因为你正在设置 $SIG{CHLD}
至 IGNORE
.这样做,你就是在杀死 qx
能够捕获 tar
的退出代码...它会在不通知父进程(您的子进程)的情况下死亡。
测试很简单:
perl -e '$SIG{CHLD} = "IGNORE"; system("ps"); print "Finished with $?\n";
这给出了 -1。
perl -e 'system("ps"); print "Finished with $?\n";
这给出了 0。
如果您真的需要
$SIG{CHLD} = 'IGNORE'
,然后只是 $SIG{CHLD} = 'DEFAULT'
在您之前 qx
称呼。另外,请确保您使用的是
tar
的完整路径。 (例如 /bin/tar
)以防万一您没有 /bin
在您的路径中,并且无法执行。但是,我假设这没问题,因为您没有说明未创建 tar 文件。
关于ajax - 在 perl 中 fork 但从子进程中的系统调用中获取退出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5813312/