这个问题与上一个问题有关: perl run background job and force job to finish?
我以为我有一个脚本可以通过执行成功杀死我的后台作业kill 15, $logger_pid
,但结果证明该命令创建了两个作业。请参阅下面的详细信息:
#!/usr/bin/perl
use strict;
# Do a background $USER top every minute
my $cmd = "top -b -d 1 -n 300 -u $ENV{USER} >> logfile";
$SIG{CHLD}="IGNORE"; my $logger_pid;
unless ($logger_pid=fork) { exec($cmd); }
# This actually creates two jobs with consecutive PIDs
# $USER 5609 0.0 0.0 63820 1072 pts/9 S 09:42 0:00 sh -c top -b -d 1 -n 300 -u $USER >> logfile
# $USER 5610 0.6 0.0 10860 1216 pts/9 S 09:42 0:00 top -b -d 1 -n 300 -u $USER
# Do something for a while
foreach my $count (1..5) { print "$count\n"; sleep 1; }
# I thought this first kill command was enough
kill 15, $logger_pid;
# This line is needed to kill the child job
kill 15, ($logger_pid+1);
1;
有人可以告诉我为什么我需要一秒钟kill 15, ($logger-pid+1)
才能真正杀死后台工作吗?有没有办法用一个单一的kill
语句来做到这一点?