Exec node question

In Node Red I can start something running using exec node..spawn mode gives correct pid... but...

I want to kill the running program via its PID. I can see the PID number in the status but nothing seems to come out of any of the 3 outputs. How can I make a note of the PID for later killing with another exec node.. rather than typiing it into the kill exec node

On Windows, you can to start processes using an exec node with:

wmic process call create \path\process.exe | find "ProcessId"

command which outputs the PID, then store it in a flow context to be able to kill it with another exec node using:

taskkill /F /PID storedID
1 Like

You can use the 'Status' node to capture the pid from the exec node.

1 Like

Also, if you have left it at the default (exec) mode it won't send anything till the exec is complete. If you select spawn mode then I think it will give some output immediately.

I did use spawn.

NEARLY Nick, it retrurns a PID number but then a second later it puts RC2 in the status and does not run the job.

/usr/bin/python /home/pi/luma.examples/examples/3d_box.py --interface spi --display ssd1351 --height 128 --width 128 | find "ProcessId"

I tried both with and without wmic process call create

As stated wmic won't work in Linux as its a windows command.

Maybe try:

my-app & echo $!

Another command that might work for you:

pidof my-app

If this is yours to be able to change, you could get it to output the PID early in the process. I'm pretty sure Python has a library to get the PID just as I think NodeJS does.

the example file isn't mine.. and sorry I wasnt thinking, obviously this is running on a pi.

I'll trying the app with arguments as original and & echo $!

python /home/pi/luma.examples/examples/3d_box.py --interface spi --display ssd1351 --height 128 --width 128 & echo $!

without the & echo $! it works as before, manuallyt noting the status from the node status I can then stop the process - but adding that on the end,
the pid comes through the node status window promptly followed by rc2 - and the process is not running. Ideas?

Cant really use pidof myapp as I call Python with different arguments to do different things. Thats on example, enother is to light up another display. I wonder why the ampersand and echo fails....

Seems like echo $! only works for background processes, maybe yours launches in the foreground?

Try launching your process with a custom name:

Hugobox.. Dont understand how I would use the above in a node-red exec node... sorry, Surely there must be a way to use exec node and return the PID - it returns it in the status for all to see but I cant see any way to get that out without using pen and ink to copy the pid.

You might want to wrap the whole thing in a shell script? The issue is that you need to get a PID before the main part of the app runs.

Maybe try in the exec node:

bash -c "exec -a MyUniqueProcessName python /home/pi/luma.examples/examples/3d_box.py --interface spi --display ssd1351 --height 128 --width 128  &"

And to kill it in another exec node:

pkill -f MyUniqueProcessName

Otherwise if your exec node returns the PID in its status but then also displays "RC2", you could catch it via a status node and then filter the output in a function by type: if its a number then store in context as your PID, if its "RC2" do not store...

This is the nearest yet - I need to break that up to extra arguments etc but that works, stores the PID and I can then kill the PID - THANKS,,,

Sorry - I'm late to the party,..

further up you said it does send the PID to status - then changes to RC2.... surely that means it's finished as you should only get the return code when it's complete ? So there should be nothing to kill ? - (And even if it does send it to status then you can surely grab it quick enough with the status node ?)

Also if it is a python process are you using -u to ensure you are getting unbuffered output ?

dceejay Leader
October 15

Sorry - I'm late to the party,..

further up you said it does send the PID to status - then changes to RC2.... surely that means it's finished as you should only get the return code when it's complete ? So there should be nothing to kill ? - (And even if it does send it to status then you can surely grab it quick enough with the status node ?)

Also if it is a python process are you using -u to ensure you are getting unbuffered output ?

The rc2 was coming out of status indicating the process was not running – and I don’t know how to get info out of a node status message (underneath) other than to physically look at it.

The bash solution works.. inject a unique name into an inject node payload along with the stuff to run:

tester python /home/pi/luma.examples/examples/3d_box.py --interface spi --display ssd1351 --height 128 --width 128

In the actual exec node: command is:

bash -c "exec –a

Followed by appending the message payload from the inject, followed by additional arguments

&"

To kill the task, put:

pkill –f

followed by the unique name.

Messy but it works a treat. Unless there’s a simpler way,

Thanks

Pete

The status node will report the status of any node you point it at :slight_smile:
image

I called one of the exec nodes “thisone” and told the status node about it. Sending the output of the status node to a debug it said “undefined”… so I looked at the entire message and indeed, msg.status.text is “pid:10039” which is then easily split. Easier to conceptualise but actually ends to with more bits than previous solution and you need to store the PID somewhere. I’ll keep that on file thanks.

Pete

1 Like