在 Perl 中启动非等待后台进程
我有一个 Perl 脚本,需要在后台启动另一个进程并退出,而不等待其他脚本完成。 StackOverflow 上有很多线程介绍如何在 Perl 中等待或如何不等待其他编程语言,但我似乎找不到 Perl 的正确答案。
我已经阅读了相当多的内容,并认为我正在做正确的事情,但我的尝试似乎都没有正常工作。以下是我迄今为止尝试过的所有变体,但均无济于事:
system(qq|perl /util/script.pl $id|);
system(qq|perl /util/script.pl $id &|);
exec(qq|perl /util/script.pl $id|);
exec(qq|perl /util/script.pl $id &|);
对于每一个变体,父进程都会继续等待子进程完成后再退出。请让我知道我做错了什么以及分叉后台进程的正确方法。
预先感谢您的帮助!
帮助调试的完整代码。请注意,API->Function() 调用是面向对象的模块,我们的代码库用于数据库交互等的特定函数:
sub Add {
my $self = shift;
my $domain = shift;
if(!$self->IsValid($domain)) { return; }
my $SQL = qq| insert into domains set added=NOW(),domain=| . API->Database->Quote($domain);
my $sth = API->DatabaseQuery($SQL);
$SQL = qq| select last_insert_id() |;
$sth = API->DatabaseQuery($SQL);
my $id = $sth->fetchrow_array();
my $command = qq|perl /home/siteuser/util/new.pl $id &|;
system($command);
return {'id'=>$id,'domain'=>$domain};
}
I have a Perl script that needs to kick off another process in the background and exit without waiting for the other script to finish. There are lots of threads on StackOverflow that cover how to wait in Perl or how to not wait for other programming languages, but I can't seem to find the right answer for Perl.
I've read up quite a bit and thought I was doing the right things but none of my attempts seem to be working correctly. Here are all the variations I've tried so far to no avail:
system(qq|perl /util/script.pl $id|);
system(qq|perl /util/script.pl $id &|);
exec(qq|perl /util/script.pl $id|);
exec(qq|perl /util/script.pl $id &|);
With each of these the parent process continues to wait for the child to finish before exiting. Please let me know what I'm doing wrong and the right way to fork the background process.
Thanks in advance for your help!
Full code to help with debugging. Note that the API->Function() calls are object oriented modules our code base uses for specific functions that database interactions, etc:
sub Add {
my $self = shift;
my $domain = shift;
if(!$self->IsValid($domain)) { return; }
my $SQL = qq| insert into domains set added=NOW(),domain=| . API->Database->Quote($domain);
my $sth = API->DatabaseQuery($SQL);
$SQL = qq| select last_insert_id() |;
$sth = API->DatabaseQuery($SQL);
my $id = $sth->fetchrow_array();
my $command = qq|perl /home/siteuser/util/new.pl $id &|;
system($command);
return {'id'=>$id,'domain'=>$domain};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个被设计为以这种方式工作 -
system
执行命令并完成以使其结束。最后两个也是这样设计的 -
exec
专门设计为永不返回。它基本上用子进程替换了父进程。然而,第二个应该可以解决问题:从系统调用启动的命令是 shell,它会给出要执行的字符串。由于字符串以“&”结尾,这意味着 shell 将以后台进程的形式启动您的命令,并在启动后完成自己的执行。
您能否发布更多代码来说明#2 如何不起作用?
另外,看看如果您尝试反引号或 qx 会发生什么:
另外,作为减少未知数的一种方法,您能否运行以下命令并告诉我打印的内容:
The first one is designed to work that way -
system
executes the command and finishes for it to end.The last two are also designed that way -
exec
specifically is designed to never return. It basically replaces the parent process with the child process.However, the second one should do the trick: the command launched from the
system
call is shell, which is given your string to execute. Since the string ends with "&", that means the shell will launch your command as a background process, and finish its own execution after that launch.Can you please post more code illustrating how #2 didn't work?
Also, see what happens if you try backticks or qx:
Also, as a way of reducing unknowns, can you please run the following and tell me what prints:
您是否在调用
system
或exec
之前调用fork()
?Are you calling
fork()
before callingsystem
orexec
?您需要解除孩子与父母的联系。
请参见 perldoc -q 守护进程。或者 Proc::Daemon
You need to disassociate the child from the parent.
See perldoc -q daemon. Or Proc::Daemon
使用 fork 是后台进程的好方法:
Using fork is a good way to background processes: