2

我有一个 Perl 脚本,它启动后台作业以将一些日志记录信息打印到文件中,执行某些操作,然后完成。

我想在脚本完成后结束后台进程,但据我所知,如果我使用如下所示的双叉,那么waitpid($pid,0) 将等待后台进程完成,而不是杀死它,并且kill(9,$pid)不会得到摆脱后台进程。

示例脚本:

#!/usr/bin/perl
use strict;

# Do a first general top
my $ret = `top -b -n 1 > logfile`;

# Do a background $USER top every minute
my $cmd = "top -b -d 60 -n 300 -u $ENV{USER} >> logfile";
my $pid;
unless ($pid = fork) {
  unless (fork) {       #second_fork
    exec "$cmd";
    die "exec failed!";
  }                     #second_fork
  exit 0;
}

# Script does something else for 1-2 hours
# [...]
# We are finished now, let's finish the logging

# waitpid($pid,0) would wait until top command is finished
force_background_job_to_finish($pid) if (background_job_still_running($pid));

# Add final top to the end
$ret = `top -b -n 1 >> logfile`;

1;

任何想法如何force_background_job_to_finish

4

2 回答 2

2

通常使用双叉,这样您就不必担心收获子进程。由于您想控制子进程,您可以只执行一次 fork,它会为您提供进程 ID,然后您可以使用它来使用 kill 杀死您的子进程。

于 2013-09-26T14:29:35.153 回答
2

这对我有用:

$SIG{CHLD}="IGNORE";
my $cmd = "top -b -d 60 -n 300 -u $ENV{USER} >> logfile";
if (my $logger_pid=fork) {
    # parent
    do_something_for_1to2_hours();
    kill 15, $logger_pid;
} else {
    # child (logger)
    exec($cmd);
}
于 2013-09-26T16:08:58.190 回答