我有一个 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
?