Using the exec node to kill and restart a process

Hey, I've got a few devices from Pimoroni, for example

This comes with some nice examples, for instance, to scroll text across the HAT.

I'm looking for a way to kill the text scrolling process (python3) and start a new one with some new text each time my flow is triggered, but it seems I can't chain a killall python3 (yes, I know!) exec node with my scrolling text script (called from a 2nd exec node).

Could anyone recommend how they might do this?

Isn't this just an spi device? Perhaps you could do it completely in node red using an spi node?

Well there is this - but no idea if it still works... https://flows.nodered.org/node/node-red-node-pi-unicorn-hat

Meanwhile you should be able to send a msg with msg.kill set to "SIGINT" (or whatever SIG you need to kill that exec. if you want. See the info bar to the right of the workspace.

Thank you, but I am still not sure how I can msg.kill the process, and then subsequently start a new one with a new msg.payload

The type of flow I'm looking for is

new text comes in -> kill the process -> start a new process

But I can't quite wrap my head around how I'd do that, seeing as I don't think I can just chain another exec node on the end of the last one.

First here is another way to kill only your one specific process that can be chained:

[{"id":"d6c16e01.a6f6a8","type":"inject","z":"20693e98.c7fb2a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":80,"wires":[["183c2cfc.9db16b"]]},{"id":"183c2cfc.9db16b","type":"exec","z":"20693e98.c7fb2a","command":"","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":450,"y":80,"wires":[[],[],[]]},{"id":"235a9087.df7c","type":"inject","z":"20693e98.c7fb2a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":140,"wires":[["20d208f2.12a6b"]]},{"id":"20d208f2.12a6b","type":"change","z":"20693e98.c7fb2a","name":"set to pid","rules":[{"t":"set","p":"payload","pt":"msg","to":"pid","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":140,"wires":[["790a721f.565ee4"]]},{"id":"790a721f.565ee4","type":"exec","z":"20693e98.c7fb2a","command":"kill ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":490,"y":140,"wires":[[],[],[]]},{"id":"c12b76ce.692f38","type":"status","z":"20693e98.c7fb2a","name":"","scope":["183c2cfc.9db16b"],"x":160,"y":200,"wires":[["2cc3e208.46e966"]]},{"id":"2cc3e208.46e966","type":"function","z":"20693e98.c7fb2a","name":"get pid","func":"if(msg.status.text){\n    let pid = msg.status.text.split(':');\n    msg.payload = pid[1];\n    return msg;\n} else {\n    return null;\n}","outputs":1,"noerr":0,"x":320,"y":200,"wires":[["8a7f851b.b5f7a"]]},{"id":"8a7f851b.b5f7a","type":"change","z":"20693e98.c7fb2a","name":"","rules":[{"t":"set","p":"pid","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":200,"wires":[[]]}]


You can use a status node to get the Process Id of a specific process und use that to just kill that one. Here the status node watches the top exec and saves it to flow.pid which in turn gets appended to the kill command.

Secondly yes you can chain multiple execs behind each other. You can even send the command for the second exec node in the msg object for the first. just put the command for the second exec node in some msg property like msg.secondCommand and than use a change node after the first exec to move this to msg.payload.

So in your case set the msg.payload to the pid and for example msg.text to your text you want to append to your python script. Pass this to the first exec with append msg.payload selected and the kill command. After this exec place a change node which moves msg.text to msg.payload and than the second exec which runs your python script also with append msg.payload selected.

You should think about also doing something like setting pid to false when the process is not running and check for this and if it’s not running skip the kill exec.

Johannes

Edit here is a flow that does that:

[{"id":"982fcecc.e486a","type":"inject","z":"5d6b1a8.91496e4","name":"","topic":"","payload":"--help","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":100,"wires":[["3754046c.b99e7c"]]},{"id":"3754046c.b99e7c","type":"switch","z":"5d6b1a8.91496e4","name":"","property":"pid","propertyType":"flow","rules":[{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":100,"wires":[["365b402.5f7b84"],["c80acf86.678408"]]},{"id":"365b402.5f7b84","type":"exec","z":"5d6b1a8.91496e4","command":"voice2json","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1410,"y":100,"wires":[[],[],[]]},{"id":"e2a0192.e8d9868","type":"delay","z":"5d6b1a8.91496e4","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1240,"y":160,"wires":[["365b402.5f7b84"]]},{"id":"588ea205.651484","type":"change","z":"5d6b1a8.91496e4","name":"","rules":[{"t":"move","p":"secondCommand","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":160,"wires":[["e2a0192.e8d9868"]]},{"id":"c288f914.3279f8","type":"exec","z":"5d6b1a8.91496e4","command":"kill","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":810,"y":160,"wires":[["588ea205.651484"],[],[]]},{"id":"c80acf86.678408","type":"change","z":"5d6b1a8.91496e4","name":"set to pid & move second command","rules":[{"t":"move","p":"payload","pt":"msg","to":"secondCommand","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"pid","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":160,"wires":[["c288f914.3279f8"]]},{"id":"b909e542.18bab8","type":"change","z":"5d6b1a8.91496e4","name":"","rules":[{"t":"set","p":"pid","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":240,"wires":[[]]},{"id":"1fd9e55a.71c003","type":"inject","z":"5d6b1a8.91496e4","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":270,"y":300,"wires":[["b909e542.18bab8"]]},{"id":"ef785fa3.a41398","type":"function","z":"5d6b1a8.91496e4","name":"get pid","func":"if(msg.status.text){\n    if(msg.status.text.match(/pid/g)){\n        let pid = msg.status.text.split(':');\n        msg.payload = pid[1];\n        return msg;\n    } else {\n        msg.payload = false;\n        return msg;\n    }\n} else {\n    msg.payload = false;\n    return msg;\n}","outputs":1,"noerr":0,"x":260,"y":240,"wires":[["b909e542.18bab8"]]},{"id":"236b0228.77bc06","type":"status","z":"5d6b1a8.91496e4","name":"","scope":["365b402.5f7b84"],"x":100,"y":240,"wires":[["ef785fa3.a41398"]]}]


of course you have to change the commands to yours.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.