我正在编写一个Perl脚本,该脚本进行系统调用以终止正在运行的进程。例如,我想杀死所有的PuTTy窗口。为了做到这一点,我有:
系统(‘TASKKILL /F /IM putty* /T 2>nul');
然而,对于每一个被杀死的过程,我都会得到一个印子
成功: PID xxxx的子进程已被终止。
把我的CLI弄乱了。消除这些指纹的简单方法是什么?还要注意,我正在Cygwin中执行这些脚本。
发布于 2011-09-15 21:11:20
重定向sderr->stdout->nul:
system('TASKKILL /F /IM putty* /T 1>nul 2>&1');或者只是简单地获取输出:
my $res = `TASKKILL /F /IM putty* /T 2>nul`;发布于 2011-09-15 21:30:26
TASKKILL写入第一个文件描述符(标准输出),而不是第二个。你想说
system('TASKKILL /F /IM putty* /T >nul');发布于 2011-09-16 16:14:39
$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;
}https://stackoverflow.com/questions/7437433
复制相似问题