Jacques44 / node-red-contrib-bigssh

remote command using ssh big node
Apache License 2.0
8 stars 12 forks source link

How to terminate long running process via bigssh #26

Open mb-software opened 3 years ago

mb-software commented 3 years ago

Hi and thanks for the great work on bigssh. I use it to start a long (endless) running process on a remote machine. However, I have found no solution how to terminate it (what I would do in a terminal by pressing Ctrl-C). The only way for me at the moment to stop / restart the process is to completely restart Nodered. Even a deploy or restart of the flow does not terminate the process.

Is there any idea/solution for this?

THanks

Jacques44 commented 3 years ago

Hello,

If I understand well, you’d like to kill and endless process on a remote host?

Maybe a special message sent to the node would break the connection….

On 9 May 2021, at 09:23, Markus @.***> wrote:

Hi and thanks for the great work on bigssh. I use it to start a long (endless) running process on a remote machine. However, I have found no solution how to terminate it (what I would do in a terminal by pressing Ctrl-C). The only way for me at the moment to stop / restart the process is to completely restart Nodered. Even a deploy or restart of the flow does not terminate the process.

Is there any idea/solution for this?

THanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Jacques44/node-red-contrib-bigssh/issues/26, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEKS6M2WO4L76VSENFL65ODTMYZ5LANCNFSM44OUYNDA.

mb-software commented 3 years ago

Yes, I start a process using a nodered flow with bigssh, later I may want to stop or restart that same process. How to do that? Sorry, Im new to big nodes ... what do you mean by "Maybe a special message sent to the node would break the connection…." what special message to send?

Jacques44 commented 3 years ago

Something like that

kill

Jacques44 commented 3 years ago

It's really hard to control remote processes as they will or won't listen to feed events. So, closing the connection will or won't kill the running process. The easy way is to print the PID while running the command, catch the PID in node-red and store it in context. Then, if you want to kill that process, you just have to set a new big-ssh node with a kill command

Jacques44 commented 3 years ago

Here is my test flow:

[{"id":"50fcc877.3d1528","type":"inject","z":"5f1eb5c5.4b8a5c","name":"fire","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":400,"wires":[["7286f343.9acdcc"]]},{"id":"7286f343.9acdcc","type":"bigssh","z":"5f1eb5c5.4b8a5c","name":"exec","commandLine":"/bin/sleep 99999 & echo $!","commandArgs":"","minError":1,"minWarning":1,"noStdin":false,"format":"","payloadIsArg":false,"myssh":"29df19bc.46db36","x":290,"y":400,"wires":[["a139f5fb.19d848"],[],["4b6c5d05.a64054"]]},{"id":"3300ce96.15c3f2","type":"inject","z":"5f1eb5c5.4b8a5c","name":"kill","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":720,"wires":[["39df8a27.519606"]]},{"id":"a139f5fb.19d848","type":"bigline","z":"5f1eb5c5.4b8a5c","name":"stdout","filename":"","format":"utf8","keepEmptyLines":false,"x":470,"y":340,"wires":[["b9708af3.9f6738","844ca284.dbd5d"],[]]},{"id":"3e6d5ad2.544316","type":"bigssh","z":"5f1eb5c5.4b8a5c","name":"killer","commandLine":"kill","commandArgs":"","minError":1,"minWarning":1,"noStdin":false,"format":"","payloadIsArg":true,"myssh":"29df19bc.46db36","x":630,"y":720,"wires":[["b9b05a0a.cb9a38"],["aa034e9c.2d252"],["5def1342.63f96c"]]},{"id":"b9708af3.9f6738","type":"function","z":"5f1eb5c5.4b8a5c","name":"store pid","func":"flow.set(\"pid\", msg.payload)\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":380,"wires":[["e81b0d49.82e68","f9e785cc.9ca958"]]},{"id":"e81b0d49.82e68","type":"debug","z":"5f1eb5c5.4b8a5c","name":"display pid","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":380,"wires":[]},{"id":"39df8a27.519606","type":"function","z":"5f1eb5c5.4b8a5c","name":"get pid","func":"// Get back pid\nmsg.payload = flow.get(\"pid\")\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":720,"wires":[["e1ef3f95.019a5"]]},{"id":"b9b05a0a.cb9a38","type":"bigline","z":"5f1eb5c5.4b8a5c","name":"stdout","filename":"","format":"utf8","keepEmptyLines":false,"x":790,"y":680,"wires":[["c7dfeff0.dea28"],[]]},{"id":"c7dfeff0.dea28","type":"debug","z":"5f1eb5c5.4b8a5c","name":"kill output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":680,"wires":[]},{"id":"e1ef3f95.019a5","type":"switch","z":"5f1eb5c5.4b8a5c","name":"defined?","property":"payload","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","repair":false,"outputs":2,"x":460,"y":720,"wires":[["3e6d5ad2.544316"],["17105d99.2baeb2"]]},{"id":"a72f21a1.43431","type":"debug","z":"5f1eb5c5.4b8a5c","name":"kill stderr","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":760,"wires":[]},{"id":"5def1342.63f96c","type":"bigline","z":"5f1eb5c5.4b8a5c","name":"stderr","filename":"","format":"utf8","keepEmptyLines":false,"x":790,"y":760,"wires":[["a72f21a1.43431"],[]]},{"id":"4b6c5d05.a64054","type":"bigline","z":"5f1eb5c5.4b8a5c","name":"stderr","filename":"","format":"utf8","keepEmptyLines":false,"x":470,"y":420,"wires":[["118588b8.06bfd7"],[]]},{"id":"118588b8.06bfd7","type":"debug","z":"5f1eb5c5.4b8a5c","name":"exec stderr","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":420,"wires":[]},{"id":"4b624430.d7586c","type":"inject","z":"5f1eb5c5.4b8a5c","name":"running","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":540,"wires":[["f9e785cc.9ca958"]]},{"id":"f9e785cc.9ca958","type":"function","z":"5f1eb5c5.4b8a5c","name":"get pid","func":"// Get back pid\nmsg.payload = flow.get(\"pid\")\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":540,"wires":[["c12e71ab.e512"]]},{"id":"c12e71ab.e512","type":"switch","z":"5f1eb5c5.4b8a5c","name":"defined?","property":"payload","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","repair":false,"outputs":2,"x":460,"y":540,"wires":[["6f8602af.304f2c"],["17105d99.2baeb2"]]},{"id":"6f8602af.304f2c","type":"bigssh","z":"5f1eb5c5.4b8a5c","name":"ps","commandLine":"ps -p","commandArgs":"","minError":1,"minWarning":1,"noStdin":false,"format":"","payloadIsArg":true,"myssh":"29df19bc.46db36","x":630,"y":540,"wires":[["a662ac8f.9710e"],["33d49a61.3e5066"],["6c5d2ca6.2b3e44"]]},{"id":"a662ac8f.9710e","type":"bigline","z":"5f1eb5c5.4b8a5c","name":"stdout","filename":"","format":"utf8","keepEmptyLines":false,"x":790,"y":500,"wires":[["36cb0b63.099344"],[]]},{"id":"36cb0b63.099344","type":"debug","z":"5f1eb5c5.4b8a5c","name":"ps output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":500,"wires":[]},{"id":"85df532.dfd70b","type":"debug","z":"5f1eb5c5.4b8a5c","name":"ps stderr","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":580,"wires":[]},{"id":"6c5d2ca6.2b3e44","type":"bigline","z":"5f1eb5c5.4b8a5c","name":"stderr","filename":"","format":"utf8","keepEmptyLines":false,"x":790,"y":580,"wires":[["85df532.dfd70b"],[]]},{"id":"2bb754a2.579b2c","type":"bigstatus","z":"5f1eb5c5.4b8a5c","name":"status","locale":"","show_date":false,"show_duration":false,"x":1310,"y":640,"wires":[[]]},{"id":"17105d99.2baeb2","type":"function","z":"5f1eb5c5.4b8a5c","name":"","func":"msg = {\n \"control\": { \"state\": \"error\", \"message\": \"no pid\" }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":640,"wires":[["2bb754a2.579b2c"]]},{"id":"33d49a61.3e5066","type":"function","z":"5f1eb5c5.4b8a5c","name":"","func":"if (msg.control.rc == 1) {\n msg.control.message = \"not running\"\n msg.control.state = \"end\"\n} else {\n msg.control.message = \"still running\"\n msg.control.state = \"start\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":560,"wires":[["2bb754a2.579b2c"]]},{"id":"aa034e9c.2d252","type":"function","z":"5f1eb5c5.4b8a5c","name":"","func":"if (msg.control.rc > 0) {\n msg.control.message = \"kill failed\"\n} else {\n msg.control.message = \"killed\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":720,"wires":[["2bb754a2.579b2c"]]},{"id":"844ca284.dbd5d","type":"debug","z":"5f1eb5c5.4b8a5c","name":"exec stdout","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":340,"wires":[]},{"id":"29df19bc.46db36","type":"SSH_Credentials","host":"127.0.0.1","port":"22","userlabel":"test@127.0.0.1"}]

mb-software commented 3 years ago

thanks, I will give it a try