等待子进程在perl的系统命令中完成

时间:2014-06-25 12:21:15

标签: perl

我脚本的一部分看起来像这样。

my @args = ("/bin/updateServer & ");
system (@args) == 0 or die "system @args failed: $?";
reloadServer;

我的要求是只在updateServer完成后才需要调用reloadServer。 在我的情况下,重新加载服务器在更新服务器之后立即运行。 UpdateServer运行大约4个小时,所以我必须在后台运行它"&"

如何在updateServer完成后更改我的代码以运行reloadServer。

有人可以帮助我这样做。

2 个答案:

答案 0 :(得分:1)

只需:

@args = ("/bin/updateServer");

从命令中删除& 以避免在后台启动过程

答案 1 :(得分:1)

不是在后台运行system命令,而是创建一个thread来运行它,然后重新加载:

use threads;

my $thread = threads->create(sub {
    my @args = ("/bin/updateServer");
    system (@args) == 0 or die "system @args failed: $?";
    reloadServer;
});
# Store $thread somewhere so you can check $thread->error/is_running for it failing/completing.
# Continue doing other things.

线程将在后台运行,并在(现在阻塞)reloadServer命令完成后运行system

相关问题