1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

add kill and start parameters to daemon node

This commit is contained in:
Dave Conway-Jones 2017-05-03 17:46:10 +01:00
parent a3211a5e80
commit 0fb8a1a2a0
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D
4 changed files with 34 additions and 17 deletions

View File

@ -31,8 +31,13 @@ The command provides 3 outputs... stdout, stderr, and return code, from the runn
If the called program stops (i.e. a return code is produced), this node can attempt
to restart the command automatically.
**Note :** when you stop Node-RED running we may not get a chance to kill the
called program so it may remain running. You <i>may</i> have to kill it manually.
Setting `msg.kill` to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the restart flag is set it will then auto restart.
Sending `msg.start` will also re-start the process.
**Note:** Some applications will automatically buffer lines of output. It is advisable to turn off this behaviour.
For example, if running a Python app, the `-u` parameter will stop the output being buffered.
For example it can be used to run and then monitor the
<a href="https://github.com/antirez/dump1090" target="_new">dump1090</a> plane

View File

@ -40,9 +40,10 @@
<p>Calls out to a long running system command. Sends <code>msg.payload</code> to stdin of the process.</p>
<p>Provides 3 outputs... stdout, stderr, and return code , from the running command.</p>
<p>If the called program stops (i.e. a return code is produced), this node can attempt to restart the command.</p>
<p><b>Note :</b> when you stop Node-RED running we may not get a chance to kill the called program so it may remain
running. You <i>may</i> have to kill it manually.</p>
<p>If running a Python app you may need to use the <code>-u</code> parameter to stop the output being buffered.</p>
<p>Setting <code>msg.kill</code> to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the
restart flag is set it will then auto restart. Sending <code>msg.start</code> will also re-start the process.</p>
<p><b>Note:</b> Some applications will automatically buffer lines of output. It is advisable to turn off this behaviour.
For example, if running a Python app, the <code>-u</code> parameter will stop the output being buffered.</p>
</script>
<script type="text/javascript">

View File

@ -15,14 +15,23 @@ module.exports = function(RED) {
function inputlistener(msg) {
if (msg != null) {
if (!Buffer.isBuffer(msg.payload)) {
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
if (typeof msg.payload !== "string") { msg.payload = msg.payload.toString(); }
if (node.cr === true) { msg.payload += "\n"; }
if (msg.hasOwnProperty("kill") && node.running) {
if (typeof msg.kill !== "string" || msg.kill.length === 0 || !msg.kill.toUpperCase().startsWith("SIG") ) { msg.kill = "SIGINT"; }
node.child.kill(msg.kill.toUpperCase());
}
else if (msg.hasOwnProperty("start") && !node.running) {
runit();
}
else {
if (!Buffer.isBuffer(msg.payload)) {
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
if (typeof msg.payload !== "string") { msg.payload = msg.payload.toString(); }
if (node.cr === true) { msg.payload += "\n"; }
}
if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
if (node.child !== null && node.running) { node.child.stdin.write(msg.payload); }
else { node.warn("Command not running"); }
}
if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
if (node.child !== null) { node.child.stdin.write(msg.payload); }
else { node.warn("Command not running"); }
}
}
@ -61,11 +70,13 @@ module.exports = function(RED) {
node.send([null,{payload:data},null]);
});
node.child.on('close', function (code) {
if (RED.settings.verbose) { node.log("ret: "+code); }
node.send([null,null,{payload:code}]);
node.child = null;
node.child.on('close', function (code,signal) {
if (RED.settings.verbose) { node.log("ret: "+code+":"+signal); }
node.running = false;
node.child = null;
var rc = code;
if (code === null) { rc = signal; }
node.send([null,null,{payload:rc}]);
node.status({fill:"red",shape:"ring",text:"stopped"});
});

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-daemon",
"version" : "0.0.11",
"version" : "0.0.14",
"description" : "A Node-RED node that runs and monitors a long running system command.",
"dependencies" : {
},