我正在编写一个Perl脚本,它会调用系统调用来终止正在运行的进程。例如,我想要杀死所有PuTTy窗口。为了做到这一点,我有:
系统('TASKKILL / F / IM putty * / T 2> nul');
然而,对于每个被杀的进程,我都会打印出来
成功:PID xxxx的PID xxxx子进程已终止。
这使我的CLI变得混乱。什么是消除这些印刷品的简单方法?另请注意,我正在Cygwin中执行这些脚本。
答案 0 :(得分:4)
重定向sderr-> stdout-> nul:
system('TASKKILL /F /IM putty* /T 1>nul 2>&1');
或只是抓住输出:
my $res = `TASKKILL /F /IM putty* /T 2>nul`;
答案 1 :(得分:0)
TASKKILL
写入第一个文件描述符(标准输出),而不是第二个。
你想说
system('TASKKILL /F /IM putty* /T >nul');
答案 2 :(得分:0)
$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;
}