# Dashboard, switch to start and stop python program

**URL:** <https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637>\
**Category:** Dashboard\
**Created:** [23 July 2020 21:46 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637 "2020-07-23T21:46:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KenjiRed](https://avatars.discourse-cdn.com/v4/letter/k/e68b1a/32.png) [@KenjiRed](https://discourse.nodered.org/u/KenjiRed)\
**Post date:** [23 July 2020 21:46 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/1 "2020-07-23T21:46:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [24 July 2020 16:50 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/2 "2020-07-24T16:50:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenjiRed](https://avatars.discourse-cdn.com/v4/letter/k/e68b1a/32.png) [@KenjiRed](https://discourse.nodered.org/u/KenjiRed)\
**Post date:** [24 July 2020 23:07 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/3 "2020-07-24T23:07:59Z")

</div>

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.

---

<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:** [25 July 2020 00:26 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/4 "2020-07-25T00:26:35Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [25 July 2020 17:04 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/5 "2020-07-25T17:04:47Z")

</div>

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:

 ![Screenshot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6bfc058885d5538c61e508e482437ae0ece8c31.png)

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

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [25 July 2020 17:15 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/6 "2020-07-25T17:15:59Z")

</div>

Forgot: the python code I run is

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

```

and the dashboard:

 ![Screenshot2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/675b773e6279127040b63a2f1b27a2b2c66c59c9.png)

---

<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:** [24 August 2020 17:16 UTC](https://discourse.nodered.org/t/dashboard-switch-to-start-and-stop-python-program/30637/7 "2020-08-24T17:16:04Z")

</div>

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