Dashboard, switch to start and stop python program

I want to make a switch which python program starts running on Raspberry Pi when it is ON and stops running when it is OFF.
I took Dashboard-Switch, Function-Switch and 2 Exec nodes.
At Dashboard-Switch I set Boolean TRUE for ON and FALSE for OFF.
At Function-Switch, I set 2 output. One is "is true" and the other is "is false"
(For the above 2, I also tried with number '0' and '1' but the result is the same)
For one Exec connected to 1st Function-switch output (True), I entered "python xxx.py" so that it starts Python Program. For the other Exec node connected to other Function-Switch put (False), I enter "kill xxx.py.

On UI, when I slide switch to ON, its python program starts. When I slide to OFF, it restarts its python program instead of stopping.

Any idea the better way to do it?

Many thanks in advance for the help

Your approach seems reasonable (I'm a beginner, and would do about the same way).
However, I suggest you check the return code and maybe periodically check that the program is actually running, and feed that info back to the dashboard.

Thanks for the comment...
I used pkill -f -SIGINT xxxx.py.
xxx may be replaced with $(ps aux | grep python) which means to take all python program.. which I have not tried in Node Red Exec.
In fact, it worked in expected manner when I unchecked "msg.payload"... I do not know exact reason for it but it seems to be acting as noise for 2nd Exec.

You can also use the status node to watch the exec node and then grab the PID from it and store it in a flow variable. Then you can have another flow that gets the PID from the global variable and sends 'kill nnn' where nnn is the PID number. Here is a sample

[{"id":"8ad88889.20b7f","type":"status","z":"79bc540c.0c3a9c","name":"","scope":["1c178d10.4959cb"],"x":160,"y":700,"wires":[["cd2f612f.afab68"]]},{"id":"5321b564.d42b1c","type":"debug","z":"79bc540c.0c3a9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":750,"y":700,"wires":[]},{"id":"cd2f612f.afab68","type":"change","z":"79bc540c.0c3a9c","name":"store PID in flow.pid","rules":[{"t":"change","p":"status.text","pt":"msg","from":"pid:","fromt":"str","to":"","tot":"str"},{"t":"set","p":"pid","pt":"flow","to":"status.text","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":700,"wires":[["5321b564.d42b1c"]]},{"id":"dc93cf66.62994","type":"inject","z":"79bc540c.0c3a9c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":740,"wires":[["e96b79ce.607078"]]},{"id":"e96b79ce.607078","type":"change","z":"79bc540c.0c3a9c","name":"Build 'kill nnn' where nnn is the PID from flow.pid","rules":[{"t":"set","p":"payload","pt":"msg","to":"kill pid","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"pid","fromt":"str","to":"pid","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":740,"wires":[["9a58e823.68f098"]]},{"id":"9a58e823.68f098","type":"exec","z":"79bc540c.0c3a9c","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":750,"y":740,"wires":[[],[],[]]}]

The funny thing is I just put this together earlier today.

I played a bit with this because I may have use for it later.
I wanted to:

  • Start and stop the python program from a switch
  • See if the program is running
  • See the output from the program
  • See the status of the program when exited
  • Not start multiple instances simultaneously

I ended up with this:

The code is at https://gist.github.com/harelabb/3d36f187f48274dbf5b880fcf51a138b

Forgot: the python code I run is

#! /usr/bin/python
import subprocess as sp
for i in range(10):
    sp.call(["sleep", "5"])

and the dashboard:

1 Like

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