2014-05-18 98 views
0

更新:编辑以使问题更容易理解。在Perl中运行后台进程

我正在创建一个脚本,它会自动分析HTTP上载文件,并将上传文件的信息(如名称,上载时间)存储到另一个数据文件中。这些信息可以在mod_security日志文件中找到。 mod_security有一个规则,我们可以将上传的文件重定向到Perl脚本。在我的情况下,Perl脚本是upload.pl。在这个Perl脚本中,我将使用ClamAV防病毒扫描上传的文件。但mod_sec只会记录上传的文件信息,如名称,Perl脚本upload.pl执行后的上传时间。但是我正在用execute.pl中的sleep(10)从upload.pl启动另一个perl脚本execute.pl。目的是execute.pl只有在upload.pl完成后才能启动它的功能。我需要执行execute.pl作为后台进程,并且upload.pl应该完成而不会等待execute.pl的输出。

但我的问题是,我已经使execute.pl在后台运行,HTTP上传等待execute.pl的完成,即使我已经使进程在后台执行。我需要upload.pl才能完成,而不必等待execute.pl的输出。该脚本在控制台中运行良好。例如,我从控制台执行perl upload.pl,完全执行upload.pl而不等待execute.pl的输出。但是当我通过apache尝试相同的时候,这意味着当我上传示例文件时,upload.pl和execute.pl都会上传完成。由于execute.pl已经作为后台进程从upload.pl中调用,所以上传过程应该完成而不需要等待execute.pl的输出。

我已经试过的方法至今都

system("cmd &") 

my $pid = fork(); 
if (defined($pid) && $pid==0) { 
    # background process 
    my $exit_code = system($command); 
    exit $exit_code >> 8; 
} 

my $pid = fork(); 
if (defined($pid) && $pid==0) { 
    exec($command); 
} 
+0

你问题很难理解!你告诉我们某个文件的名字,但是再也不会引用它,并且你指的是“第一个文件”和“第二个文件”,但从不说哪个是哪个文件。您引用了一个“执行某些事情”的脚本,并告诉我们您将获得“控制台访问”权限,但不是您想要的,也不是您需要“记录器记录日志”的原因。 –

+0

@mark我做了更改 – Nijin

+0

不知道你在问什么。你说你的问题是你让一些进程等待你的两个脚本完成执行。如果这确实是你的问题,那么它的答案似乎是“然后拿出你要做的代码!”但我怀疑这根本不是你的问题。 – ikegami

回答

0

问题的另一种方式:
如何启动一个Perl守护程序进程用perl我们bscript?

答:
关键是要关闭后台作业的数据流,因为它们共享:

Webscript:

#!/usr/bin/perl 

#print html header 
print <<HTML; 
Content-Type: text/html 

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html><head><title>Webscript</title> 
<meta http-equiv="refresh" content="2; url=http://<your host>/webscript.pl" /> 
</head><body><pre> 
HTML 

#get the ps headers 
print `ps -ef | grep STIME | grep -v grep`; 

#get the running backgroundjobs 
print `ps -ef | grep background.pl | grep -v grep`; 

#find any running process... 
$output = `cat /tmp/background.txt`; 

#start the background job if there is no output yet 
`<path of the background job>/background.pl &` unless $output; 

#print the output file 
print "\n\n---\n\n$output</pre></body></html>"; 

后台作业:

#!/usr/bin/perl 

#close the shared streams! 
close STDIN; 
close STDOUT; 
close STDERR; 

#do something usefull! 
for ($i = 0; $i < 20; $i++) { 
    open FILE, '>>/tmp/background.txt'; 
    printf FILE "Still running! (%2d seconds)\n", $i; 
    close FILE; 

    #getting tired of all the hard work... 
    sleep 1; 
} 

#done 
open FILE, '>>/tmp/background.txt'; 
printf FILE "Done! (%2d seconds)\n", $i; 
close FILE; 
+0

对不起,这对我无效 – Nijin

+0

完全改变了解决方案,这次测试。请让我知道这对你有没有用。 – Guido