【问题标题】:Javascript - how to find the PID where match found?Javascript - 如何找到匹配的PID?
【发布时间】:2016-06-29 09:18:41
【问题描述】:

我有以下字符串作为缓冲区(它们是具有随机列值的随机行)。

如何从找到的 ROW 中准确搜索可用 ROW 中的“警告”,如何过滤 PID?

var exec = require('child_process').exec;
exec('TASKLIST /v /FI "IMAGENAME eq python*"', function(a,b,c) {
  console.log(b);
  // Which PID contain the word : "Warnning" ? can you list them chronologically?


});

输出:

Image Name                     PID Session Name        Session#    Mem Usage Status          User Name                                              CPU Time Window Title
========================= ======== ================ =========== ============ =============== ================================================== ============ =====================================================
python.exe                    9999 RDP-Tcp#10                 1     19?556 K Running         sun\tpt                                                 0:00:15 Warnning of windows
python.exe                    3861 RDP-Tcp#10                 1     19?556 K Running         sun\tpt                                                 0:00:15 Cancle this crap
python.exe                    8080 RDP-Tcp#10                 1     19?556 K Running         sun\tpt                                                 0:00:15 Warnning of windows

【问题讨论】:

  • 您可以使用var lines = b.split('\r\n'); 或最好使用var endOfLine = require('os').EOL; var lines = b.split(endOfLine); 以行结尾拆分字符串,然后在for 循环中检查行是否包含警告if(lines[i].indexOf('Warning') > -1) console.log(lines[i]); 现在要获取pid,您可能需要正则表达式,但我'不擅长那个:(

标签: javascript node.js windows command-line


【解决方案1】:

在 exec 过滤器中提供必要的过滤器来代替 Running。

exec('TASKLIST /v /fi "STATUS eq Running" /FI "IMAGENAME eq Shoretel*" /FO csv /NH', 
    function(a,b,c) {

      // function to get the pids
      function getPID(csv){
          var pids = [];
          var lines=csv.split("\n");
          for(var i=0;i<lines.length - 1;i++){
              var headers=lines[i].split('",');
              pids.push(headers[1].split('"')[1]);
          }
          return pids;
      }

      // to get the pids
      getPID(b);
});

【讨论】:

    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2012-05-12
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多