【问题标题】:How to kill a tail process that's execute using nodejs ssh2?如何杀死使用nodejs ssh2执行的尾进程?
【发布时间】:2013-02-07 08:25:53
【问题描述】:

如何杀死我在这个查询中执行的尾部进程?

var c = new Ssh2();
   c.on('connect', function() {
     console.log('Connection :: connect');
   });
   c.on('ready', function() {
     console.log('Connection :: ready');
     c.exec('tail -f test.txt', function(err, stream) {
    if (err) throw err;
    stream.on('data', function(data, extended) {
        console.log((extended === 'stderr' ? 'STDERR: ' : '')
                   + data);
    });
    stream.on('exit', function(code, signal) {
        c.end();
    });
     });
   });
   c.on('error', function(err) {
     console.log('Connection :: error :: ' + err);
   });
   c.on('end', function() {
     console.log('Connection :: end');
   });
   c.on('close', function(had_error) {
     console.log('Connection :: close');
   });        
   c.connect({
     host: '127.0.0.1',
     port: 22,
     username: 'test',
     password: 'test'
   });

或者有什么建议使用 nodejs 执行tail -f

【问题讨论】:

    标签: linux node.js ssh tail


    【解决方案1】:

    最简单的方法是在命令行中使用这样的形式:'tail -f test.txt & read;杀死$!'。

    那么当你想杀死进程时,只需stream.write('\n');

    【讨论】:

    • 感谢您的想法,但我没有使用stream.write('\n');,而是使用c.exec("\n", function(err, stream) { c.end(); });
    • 执行c.exec("\n"...); 只会在服务器上执行另一个不必要的命令。 stream.on('exit', function(code, signal) { c.end(); }); stream.write('\n'); 应该在不执行额外命令的情况下为您提供相同的结果。
    • 请注意此解决方案在连接断开时会出现问题。该进程仍将挂在服务器上。所以目前我正在执行另一个kill 进程,而不是仅仅结束当前的cmd。
    • 这很好用,也是一个简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2018-05-07
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多