# A conundrum with exec node and kill

**URL:** <https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981>\
**Category:** General\
**Created:** [8 June 2020 09:11 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981 "2020-06-08T09:11:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 June 2020 09:11 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/1 "2020-06-08T09:11:07Z")

</div>

Can anyone explain what is going on here?  
If, in a terminal, I run `sleep 10` and then hit Ctrl-C it interrupts the sleep. However, if I use an exec node to run the sleep and kill it with `msg.kill` set to SIGINT then the exec node shows killed immediately, but the sleep does not complete until it has reached its time, at which point the exec node outputs the error message saying that it has been killed. In fact even SIGKILL does not terminate it immediately.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8fec0a7ea61c12fa4ce32ef3995a2b94947a5cad.png)

```auto
[{"id":"1676120a.9e7bd6","type":"debug","z":"8c99ec05.74e658","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":617,"y":351,"wires":[]},{"id":"7ec4abd2.2fbbb4","type":"debug","z":"8c99ec05.74e658","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":634,"y":427,"wires":[]},{"id":"4121e880.40bb18","type":"debug","z":"8c99ec05.74e658","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":634,"y":487,"wires":[]},{"id":"e60d0906.e92e8","type":"inject","z":"8c99ec05.74e658","name":"kill","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"payload":"","payloadType":"date","x":105,"y":346,"wires":[["4f93c8c1.57e2b"]]},{"id":"30e1839a.cade6c","type":"exec","z":"8c99ec05.74e658","command":"sleep 10","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":420,"y":423,"wires":[["1676120a.9e7bd6"],["7ec4abd2.2fbbb4"],["4121e880.40bb18"]]},{"id":"127167a5.30506","type":"inject","z":"8c99ec05.74e658","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":123,"y":426,"wires":[["30e1839a.cade6c","1676120a.9e7bd6"]]},{"id":"4f93c8c1.57e2b","type":"change","z":"8c99ec05.74e658","name":"","rules":[{"t":"set","p":"kill","pt":"msg","to":"SIGINT","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":249,"y":345,"wires":[["30e1839a.cade6c"]]}]

```

---

<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:** [8 June 2020 10:14 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/2 "2020-06-08T10:14:52Z")

</div>

Is this windows?

Perhaps it a similar situation where a child process is spawned & you need to send a negative PID (mentioned in a similar topic just recently regarding VLC)

In windows task manager I see...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/163ba2c3303addbb19f66f9ad3998a025b3788be.png)

### EDIT

tried minus pid - didnt kill sleep.exe on windows.  
Also noted, windows generates a cmd.exe (thats the pid status shown) and a sleep.exe process. Even if you kill cmd.exe (in taskman), sleep.exe continues to run.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4da4d63b63b72821e6444d328ab180e259cb2cf0.png)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [8 June 2020 10:15 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/3 "2020-06-08T10:15:07Z")

</div>

Hey Colin,  
Could [this](https://unix.stackexchange.com/questions/478563/sleep-wait-and-ctrlc-propagation) perhaps explain it? Not sure...  
Bart

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 June 2020 10:25 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/4 "2020-06-08T10:25:50Z")

</div>

Try this - it grabs the PID number using the status node and stuffs it into a flow variable. The second flow formats a kill command and tuns it.

 ![Screen Shot 2020-06-08 at 6.26.15 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d1e39e17c9420704dcfefa74190fc5c31a9d9e86.png)

```auto
[{"id":"9aa9f124.4c468","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":240,"wires":[]},{"id":"38586e72.a275e2","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":280,"wires":[]},{"id":"fc7a51a1.51f46","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":320,"wires":[]},{"id":"7c21f08f.6ba39","type":"inject","z":"5b02c2f0.56c2a4","name":"kill","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":520,"wires":[["41adb3aa.d6c0dc"]]},{"id":"afca22ee.3ecac8","type":"exec","z":"5b02c2f0.56c2a4","command":"sleep 1000","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"sleep test","x":460,"y":300,"wires":[["9aa9f124.4c468"],["38586e72.a275e2"],["fc7a51a1.51f46"]]},{"id":"897b57a6.6332c","type":"inject","z":"5b02c2f0.56c2a4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":240,"wires":[["9aa9f124.4c468","afca22ee.3ecac8"]]},{"id":"41adb3aa.d6c0dc","type":"change","z":"5b02c2f0.56c2a4","name":"term","rules":[{"t":"set","p":"payload","pt":"msg","to":"'kill -15 '&$flowContext('pid')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":520,"wires":[["87d8dbee.7ade6"]]},{"id":"bda82b52.2a2cc8","type":"catch","z":"5b02c2f0.56c2a4","name":"","scope":null,"uncaught":false,"x":280,"y":160,"wires":[["4dfb7e75.be88e8"]]},{"id":"4dfb7e75.be88e8","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":530,"y":160,"wires":[]},{"id":"87d8dbee.7ade6","type":"exec","z":"5b02c2f0.56c2a4","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":510,"y":520,"wires":[["f410d490.5a00f"],["64653835.51bf9"],["f1594942.ebd3f"]]},{"id":"f410d490.5a00f","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":480,"wires":[]},{"id":"64653835.51bf9","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":520,"wires":[]},{"id":"f1594942.ebd3f","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":560,"wires":[]},{"id":"16a831c8.b69646","type":"debug","z":"5b02c2f0.56c2a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":380,"wires":[]},{"id":"163c73bd.50b0e4","type":"status","z":"5b02c2f0.56c2a4","name":"","scope":["afca22ee.3ecac8"],"x":240,"y":380,"wires":[["f1723219.09482"]]},{"id":"f1723219.09482","type":"change","z":"5b02c2f0.56c2a4","name":"","rules":[{"t":"set","p":"pid","pt":"flow","to":"$number($split(status.text, ':')[1])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":380,"wires":[["16a831c8.b69646"]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 June 2020 10:36 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/5 "2020-06-08T10:36:41Z")

</div>

You are right, having investigated further I can see that the exec node starts a shell to run the command, so looking at the active processes I see

```auto
   27958 0.0 0.0 2600 724 ? SN 11:31 0:00 /bin/sh -c sleep 30
   27959 0.0 0.0 8380 696 ? SN 11:31 0:00 sleep 30

```

and the kill command only kills the shell not the sleep.  
There is a very simple fix and that is to run the command in spawn mode, in which case the exec node runs sleep directly and everything works as expected.

---

<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:** [8 June 2020 10:37 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/6 "2020-06-08T10:37:54Z")

</div>

> [@Colin](#):
>
> run the command in spawn mode

What are the implications of that?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 June 2020 10:42 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/7 "2020-06-08T10:42:22Z")

</div>

@Steve-Mcl It just means any output from the command comes 'on the fly' and if you need to know when the command has completed then you have to use the output from o/p 3 to indicate that. For the actual command I am trying to run that is not an issue.

A corollary is that the optional timeout setting in the exec mode only works in spawn mode. At least that is what I am seeing.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 June 2020 10:49 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/8 "2020-06-08T10:49:18Z")

</div>

I am a bit confused now, looking at the info for the exec node it says that in the default mode it uses the `exec` system call, but the docs for that on linux say that this replaces the running process with the new one, keeping the same pid, so that doesn't make sense.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 June 2020 10:06 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/9 "2020-06-09T10:06:44Z")

</div>

I have started a [new thread](https://discourse.nodered.org/t/exec-node-timeout-not-working-in-exec-mode/28040) to report the apparent negative feature with the Timeout setting in the Exec node

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 June 2020 10:06 UTC](https://discourse.nodered.org/t/a-conundrum-with-exec-node-and-kill/27981/10 "2020-06-23T10:06:46Z")

</div>

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