我正在编写一个 Perl 脚本,它可以进行系统调用以终止正在运行的进程。例如,我想杀死所有 PuTTy 窗口。为了做到这一点,我有:
system('TASKKILL /F /IM putty* /T 2>nul');
然而,对于每个被杀死的进程,我得到一个打印说
SUCCESS:PID xxxx 的 PID xxxx 子进程已终止。
这使我的 CLI 变得混乱。消除这些印记的简单方法是什么?另请注意,我正在 Cygwin 中执行这些脚本。
重定向 sderr->stdout->nul:
system('TASKKILL /F /IM putty* /T 1>nul 2>&1');
或者只是简单地获取输出:
my $res = `TASKKILL /F /IM putty* /T 2>nul`;
$exec_shell='TASKKILL /F /IM putty* /T 2>nul';
my $a = run_shell($exec_shell);
#i use this function:
sub run_shell {
my ($cmd) = @_;
use IPC::Open3 'open3';
use Carp;
use English qw(-no_match_vars);
my @args = ();
my $EMPTY = q{};
my $ret = undef;
my ( $HIS_IN, $HIS_OUT, $HIS_ERR ) = ( $EMPTY, $EMPTY, $EMPTY );
my $childpid = open3( $HIS_IN, $HIS_OUT, $HIS_ERR, $cmd, @args );
$ret = print {$HIS_IN} "stuff\n";
close $HIS_IN or croak "unable to close: $HIS_IN $ERRNO";
; # Give end of file to kid.
if ($HIS_OUT) {
my @outlines = <$HIS_OUT>; # Read till EOF.
$ret = print " STDOUT:\n", @outlines, "\n";
}
if ($HIS_ERR) {
my @errlines = <$HIS_ERR>; # XXX: block potential if massive
$ret = print " STDERR:\n", @errlines, "\n";
}
close $HIS_OUT or croak "unable to close: $HIS_OUT $ERRNO";
#close $HIS_ERR or croak "unable to close: $HIS_ERR $ERRNO";#bad..todo
waitpid $childpid, 0;
if ($CHILD_ERROR) {
$ret = print "That child exited with wait status of $CHILD_ERROR\n";
}
return 1;
}
TASKKILL
写入第一个文件描述符(标准输出),而不是第二个。你想说
system('TASKKILL /F /IM putty* /T >nul');