为了在grunt插件上运行ssh,使用ssh2,我创建了一个任务,但是在稍后的connect()之前,在事件发生时,该进程将被终止。可能导致这种情况的原因是什么?
正常程序没有问题,你不想使用grunt.js。 。
Gruntfile
ssh2sample: {
default_options: {
options: {
'host': 'xxx.xxx.xxx.xxx',
'port': 22,
'username': 'names',
'privateKey': 'keys',
'passphrase': 'pass'
},
任务/ ssh2sample.js
'use strict';
module.exports = function(grunt) {
// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks
grunt.registerMultiTask('ssh2sample', 'The best Grunt plugin ever.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var Connection = require('ssh2');
var options = this.options();
options.privateKey = require('fs').readFileSync(options.privateKey);
var c = new Connection();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
console.log('Connection :: ready');
c.exec('uptime', function(err, stream) {
//if (err) throw err;
stream.on('data', function(data, extended) {
console.log(extended === 'stderr' ? 'STDERR: ' : 'STDOUT: '+ data);
});
stream.on('end', function() {
console.log('Stream :: EOF');
});
stream.on('close', function() {
console.log('Stream :: close');
});
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + 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(options);
}
});
没问题ssh2connect
var Connection = require('ssh2');
var options = this.options();
options.privateKey = require('fs').readFileSync(options.privateKey);
var c = new Connection();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
console.log('Connection :: ready');
c.exec('uptime', function(err, stream) {
//if (err) throw err;
stream.on('data', function(data, extended) {
console.log(extended === 'stderr' ? 'STDERR: ' : 'STDOUT: '+ data);
});
stream.on('end', function() {
console.log('Stream :: EOF');
});
stream.on('close', function() {
console.log('Stream :: close');
});
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + 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');
});
var key = require(fs).readdirSync(keys);
c.connect({
'host': 'xxx.xxx.xxx.xxx',
'port': 22,
'username': 'names',
'privateKey': key,
'passphrase': 'pass'
});
答案 0 :(得分:4)
默认情况下,Grunt任务是同步的。如果您的任务是异步的(就像您的任务一样),您必须告诉它等待var done = this.async();
,然后在任务完成时致电done();
。
请参阅:http://gruntjs.com/frequently-asked-questions#why-doesn-t-my-asynchronous-task-complete