A conundrum with exec node and kill

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.

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

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

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

Hey Colin,
Could this perhaps explain it? Not sure...
Bart

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.

[{"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"]]}]
1 Like

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

   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.

4 Likes

What are the implications of that?

@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.

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.

I have started a new thread to report the apparent negative feature with the Timeout setting in the Exec node

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