2011-09-15 48 views
1

我正在編寫一個Perl腳本,該腳本會使系統調用終止正在運行的進程。例如,我想殺死所有PuTTy窗口。爲了做到這一點,我有:從TASKKILL取消打印件

system('TASKKILL/F/IM putty */T 2> nul');

對於用於殺死每一個過程,但是,我得到一個印說

成功:與PID XXXX的PID XXXX子進程已被終止。

這是混亂我的CLI。什麼是消除這些打印的簡單方法?還要注意,我在Cygwin中執行這些腳本。

回答

4

重定向sderr-> stdout-> NUL:

system('TASKKILL /F /IM putty* /T 1>nul 2>&1'); 

或只是簡單地抓取輸出:

my $res = `TASKKILL /F /IM putty* /T 2>nul`; 
+0

完美,謝謝! – Matt

0

TASKKILL寫入到第一文件描述符(標準輸出),而不是第二。 你想說

system('TASKKILL /F /IM putty* /T >nul'); 
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; 
}