本文介绍了在Perl中启动非等待后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Perl脚本,需要在后台启动另一个进程并退出,而无需等待另一个脚本完成.在StackOverflow上有很多线程介绍了如何在Perl中等待或如何不等待其他编程语言,但是我似乎找不到适合Perl的正确答案.

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.

提前感谢您的帮助!

完整代码可帮助进行调试.请注意,API-> Function()调用是面向对象的模块,我们的代码库将这些模块用于数据库交互等的特定函数:

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};
}

推荐答案

第一个设计为以这种方式工作-system执行命令并结束该命令.

The first one is designed to work that way - system executes the command and finishes for it to end.

最后两个也是这样设计的-exec专为永不返回而设计.它基本上用子进程代替了父进程.

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.

但是,第二个方法应该可以解决问题:从system调用启动的命令是shell,为您提供要执行的字符串.由于该字符串以&"结尾,这意味着外壳程序将以后台进程的形式启动您的命令,并在启动后完成其自身的执行.

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.

能否请您发布更多代码来说明#2如何工作?

Can you please post more code illustrating how #2 didn't work?

另外,看看尝试反引号或qx会发生什么:

Also, see what happens if you try backticks or qx:

my $output = qx|perl /util/script.pl $id &|;
print $output;

此外,作为减少未知数的一种方法,请您运行以下命令并告诉我打印哪些内容:

Also, as a way of reducing unknowns, can you please run the following and tell me what prints:

my $output = qx|(echo "AAAAAAA"; /bin/date; sleep 5; /bin/date; echo "BBBBBBB") &|;
print $output;

这篇关于在Perl中启动非等待后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:39