diff --git a/bin/pm2 b/bin/pm2 index 37bc5c82..a3b89870 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -578,6 +578,15 @@ commander.command('dump') pm2.dump(); })); +// +// Save processes to file +// +commander.command('send ') + .description('send stdin to ') + .action(function(pm_id, line) { + pm2.sendLineToStdin(pm_id, line); + }); + // // Resurrect // diff --git a/examples/stdin.js b/examples/stdin.js new file mode 100644 index 00000000..5037d47a --- /dev/null +++ b/examples/stdin.js @@ -0,0 +1,17 @@ + +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.prompt(); + +rl.on('line', (data) =>{ + console.log('Received data'); + console.log(data); +}); + +setInterval(() => { +}, 100); diff --git a/lib/API/Extra.js b/lib/API/Extra.js index 0c812423..aecbc839 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -86,6 +86,31 @@ module.exports = function(CLI) { } }; + /** + * Description + * @method sendDataToProcessId + */ + CLI.prototype.sendLineToStdin = function(pm_id, line, cb) { + var that = this; + + var packet = { + id : pm_id, + line : line + }; + + that.Client.executeRemote('sendLineToStdin', { + pm_id : pm_id, + line : line + }, function(err, res) { + if (err) { + Common.printError(cst.PREFIX_MSG_ERR + err); + return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT); + } + Common.printOut(cst.PREFIX_MSG + 'Line "%s" sent to process id "%s"', line, pm_id); + return cb ? cb(null, res) : that.speedList(); + }); + }; + /** * Description * @method sendDataToProcessId diff --git a/lib/Daemon.js b/lib/Daemon.js index 9ba79207..d5608590 100644 --- a/lib/Daemon.js +++ b/lib/Daemon.js @@ -187,6 +187,7 @@ Daemon.prototype.innerStart = function(cb) { restartProcessId : God.restartProcessId, deleteProcessId : God.deleteProcessId, + sendLineToStdin : God.sendLineToStdin, softReloadProcessId : God.softReloadProcessId, reloadProcessId : God.reloadProcessId, duplicateProcessId : God.duplicateProcessId, diff --git a/lib/God/ActionMethods.js b/lib/God/ActionMethods.js index bb4b5557..66236dcf 100644 --- a/lib/God/ActionMethods.js +++ b/lib/God/ActionMethods.js @@ -518,7 +518,6 @@ module.exports = function(God) { } if (env) { - //env.pm2_env.env.watch = !env.pm2_env.env.watch; env.pm2_env.watch = !env.pm2_env.watch; if (env.pm2_env.watch) God.watch.enable(env.pm2_env); @@ -596,6 +595,42 @@ module.exports = function(God) { return cb(null, {}); }; + /** + * Send Line To Stdin + * @method sendLineToStdin + * @param Object packet + * @param String pm_id Process ID + * @param String line Line to send to process stdin + */ + God.sendLineToStdin = function(packet, cb) { + if (typeof(packet.pm_id) == undefined || !packet.line) + return cb(God.logAndGenerateError('pm_id or line field is missing'), {}); + + var pm_id = packet.pm_id; + var line = packet.line; + var proc = God.clusters_db[pm_id]; + + if (!proc) + return cb(God.logAndGenerateError('Process with ID <' + pm_id + '> unknown.'), {}); + + if (proc.pm2_env.exec_mode == 'cluster_mode') + return cb(God.logAndGenerateError('Cannot send line to processes in cluster mode'), {}); + + if (proc.pm2_env.status != cst.ONLINE_STATUS && proc.pm2_env.status != cst.LAUNCHING_STATUS) + return cb(God.logAndGenerateError('Process with ID <' + pm_id + '> offline.'), {}); + + try { + proc.stdin.write(line + '\n', function() { + return cb(null, { + pm_id : pm_id, + line : line + }); + }); + } catch(e) { + return cb(God.logAndGenerateError(e), {}); + } + } + God.sendDataToProcessId = function(packet, cb) { if (typeof(packet.id) == undefined || !packet.data || @@ -608,10 +643,10 @@ module.exports = function(God) { var proc = God.clusters_db[pm_id]; if (!proc) - return cb(God.logAndGenerateError('Process with ID ', pm_id, ' unknown!'), {}); + return cb(God.logAndGenerateError('Process with ID <' + pm_id + '> unknown.'), {}); if (proc.pm2_env.status != cst.ONLINE_STATUS && proc.pm2_env.status != cst.LAUNCHING_STATUS) - return cb(God.logAndGenerateError('Process with ID ', pm_id, ' unknown!'), {}); + return cb(God.logAndGenerateError('Process with ID <' + pm_id + '> offline.'), {}); try { proc.send(packet); diff --git a/test/fixtures/stdin/stdin.js b/test/fixtures/stdin/stdin.js new file mode 100644 index 00000000..5cc6c9d9 --- /dev/null +++ b/test/fixtures/stdin/stdin.js @@ -0,0 +1,16 @@ + +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.prompt(); + +rl.on('line', (line) =>{ + console.log('Line %s received', line); +}); + +setInterval(() => { +}, 100);