# Using the exec node to kill and restart a process

**URL:** <https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893>\
**Category:** General\
**Created:** [1 May 2020 21:10 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893 "2020-05-01T21:10:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wdmtech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wdmtech/32/21053_2.png) [@wdmtech](https://discourse.nodered.org/u/wdmtech)\
**Post date:** [1 May 2020 21:10 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893/1 "2020-05-01T21:10:25Z")

</div>

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

> **[Unicorn HAT HD - 256 \* RGB LED Matrix on your Raspberry Pi – Pimoroni Store](https://shop.pimoroni.com/products/unicorn-hat-hd)**
>
> We've stepped up our LED game! Unicorn HAT HD crams 256 RGB LEDs, in a 16x16 matrix, onto a single HAT. High-definition rainbow goodness!

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 May 2020 21:31 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893/2 "2020-05-01T21:31:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 May 2020 21:34 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893/3 "2020-05-01T21:34:17Z")

</div>

Well there is this - but no idea if it still works... [https://flows.nodered.org/node/node-red-node-pi-unicorn-hat](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.

---

<div class="post-metadata">

**Author:** ![wdmtech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wdmtech/32/21053_2.png) [@wdmtech](https://discourse.nodered.org/u/wdmtech)\
**Post date:** [1 May 2020 23:05 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893/4 "2020-05-01T23:05:54Z")

</div>

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`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/602e3b17001d354a0d9184240da860751f4bcc3e.png)

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.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [2 May 2020 07:42 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893/5 "2020-05-02T07:42:25Z")

</div>

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

```auto
[{"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":[[]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a83120ace416149eb1b0dfb3dbd40012b5d888a0.jpeg)  
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:

```auto
[{"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"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46640889fc2105b9364f5df10d9e3991dacaa2cd.jpeg)  
of course you have to change the commands to yours.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 July 2020 07:42 UTC](https://discourse.nodered.org/t/using-the-exec-node-to-kill-and-restart-a-process/25893/6 "2020-07-01T07:42:29Z")

</div>

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