Suppress prints from TASKKILL

老子叫甜甜 提交于 2019-12-11 10:05:50

问题


I'm writing a Perl script that makes system calls to kill running processes. For instance, I want to kill all PuTTy windows. In order to do this, I have:

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

For each process that's killed, however, I get a print saying

SUCCESS: The process with PID xxxx child of PID xxxx has been terminated.

which is cluttering my CLI. What's an easy way to eliminate these prints? Also note, that I'm executing these scripts in Cygwin.


回答1:


Redirect sderr->stdout->nul:

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

or just simply grab output:

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



回答2:


TASKKILL writes to the first file descriptor (standard output), not the second. You want to say

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



回答3:


$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/suppress-prints-from-taskkill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!