在 Perl 中启动非等待后台进程

发布于 2024-10-07 06:22:19 字数 1074 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

因为看清所以看轻 2024-10-14 06:22:19

第一个被设计为以这种方式工作 - system 执行命令并完成以使其结束。

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

然而,第二个应该可以解决问题:从系统调用启动的命令是 shell,它会给出要执行的字符串。由于字符串以“&”结尾,这意味着 shell 将以后台进程的形式启动您的命令,并在启动后完成自己的执行。

您能否发布更多代码来说明#2 如何不起作用?

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

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

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

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

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:

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;
香草可樂 2024-10-14 06:22:19

您是否在调用 systemexec 之前调用 fork()

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) {
    # background process
    exec( $command );
    # doesn't^H^H^H^H^H^H shouldn't return
}

Are you calling fork() before calling system or exec?

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) {
    # background process
    exec( $command );
    # doesn't^H^H^H^H^H^H shouldn't return
}
孤独陪着我 2024-10-14 06:22:19

您需要解除孩子与父母的联系。
请参见 perldoc -q 守护进程。或者 Proc::Daemon

You need to disassociate the child from the parent.
See perldoc -q daemon. Or Proc::Daemon

初见终念 2024-10-14 06:22:19

使用 fork 是后台进程的好方法:

my $pid = fork;
die "fork failed" unless defined $pid;
if ($pid == 0) {
    # child process goes here
    do '/util/script.pl';
    exit;
}
# parent process continues here

Using fork is a good way to background processes:

my $pid = fork;
die "fork failed" unless defined $pid;
if ($pid == 0) {
    # child process goes here
    do '/util/script.pl';
    exit;
}
# parent process continues here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文