# How to use Node-Red to check if a program has started?

**URL:** <https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439>\
**Category:** General\
**Created:** [27 June 2023 13:12 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439 "2023-06-27T13:12:03Z")\
**Posts on this page:** 7\
**Page:** 2

<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:** [28 June 2023 20:26 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/21 "2023-06-28T20:26:14Z")

</div>

In that case I don't know why you still have the problem. I think you need a Windows expert here, which I am not.

---

<div class="post-metadata">

**Author:** ![LuckyMooon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/luckymooon/32/80950_2.png) [@LuckyMooon](https://discourse.nodered.org/u/LuckyMooon)\
**Post date:** [28 June 2023 21:47 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/22 "2023-06-28T21:47:32Z")

</div>

Ok, anyway, thank you very much for your effort and helpfulness

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [29 June 2023 04:46 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/23 "2023-06-29T04:46:28Z")

</div>

> [@LuckyMooon](#):
>
> Did I understand correctly that the script lists all processes and then sends them one after the other via MQTT?

Yes, that is correct

> [@LuckyMooon](#):
>
> f this is the case, is there a way to check just one or more specific processes

There are several ways doing that. The simple example I gave obviously sends all found processes. Introducing filtering can be done either in the script or in Node-RED. To avoid sending many unnecessary MQTT messages, filtering in the script is best. There you can keep a list of processes you would like to monitor (see updated script below)  
_EDIT: I did put the script in my user directory, otherwise you have to give the full path to it in the exec node_

> [@LuckyMooon](#):
>
> how to automate the script

Also here there are several ways to achieve this. One is to introduce a loop inside the script. Another is to use Nod-RED to handle this. The flow example below is doing exactly this. It runs the script every 30 seconds and if any of the watched processes is not found, you get a message that you can use to trigger other actions like notifications, restart of the failed process etc etc (the time settings for the check needs to be modified according to your needs)  
_EDIT: We may get some comments to this flow. It is also possible to take the output directly from the exec node instead of messing around with MQTT. However, I do prefer using MQTT in this case since it gives you better flexibility if you would like to distribute "things" on several computers; you could imagine having the process checker on multiple computers and having one common Node-RED instance monitoring and presenting them all. Or several subscribers could be interested in the published information_

```auto
#!/usr/bin/python
# Version adopted for Windows
# Usage: python process_checker.py

# import the necessary packages
import paho.mqtt.client as mqtt
import subprocess
import time

broker = "192.168.0.233"
topic = "processes"
to_watch = ["Notepad.exe", "uedit32.exe"]

client = mqtt.Client()
resp = client.connect(broker, 1883, 60)

p = subprocess.Popen(['tasklist'], stdout=subprocess.PIPE)
out, err = p.communicate()

for line in out.splitlines():
    line = line.decode('windows-1252').strip('\x00')
    line = str(line).split(' ')[0]
# print (line)
    for p in to_watch:
        if p in line:
# print (p) 
            client.publish(topic, line)
            time.sleep(0.1)

client.disconnect()
exit()

```

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

```auto
[{"id":"d31a81ac.459b1","type":"tab","label":"Process checking","disabled":false,"info":""},{"id":"b9ee5961.c180e8","type":"inject","z":"d31a81ac.459b1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":150,"y":190,"wires":[["10e5e12a.16592f"]]},{"id":"10e5e12a.16592f","type":"exec","z":"d31a81ac.459b1","command":"python process_checker.py","addpay":"","append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":400,"y":190,"wires":[[],[],[]]},{"id":"622915d7.e04f6c","type":"debug","z":"d31a81ac.459b1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":400,"y":390,"wires":[]},{"id":"1b826cd3.81c033","type":"mqtt in","z":"d31a81ac.459b1","name":"","topic":"processes","qos":"0","datatype":"auto","broker":"4606489c.2375c8","nl":false,"rap":true,"rh":0,"x":150,"y":310,"wires":[["8e37eb55.c02e48","936f7d79.a90b9"]]},{"id":"4ec79b4c.22e614","type":"trigger","z":"d31a81ac.459b1","name":"","op1":"","op2":"Process not found","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":620,"y":310,"wires":[["b2ab9000.6634c"]]},{"id":"8e37eb55.c02e48","type":"switch","z":"d31a81ac.459b1","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Notepad.exe","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":310,"wires":[["4ec79b4c.22e614","622915d7.e04f6c"]]},{"id":"b2ab9000.6634c","type":"debug","z":"d31a81ac.459b1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":390,"wires":[]},{"id":"31ed49d3.b9f836","type":"debug","z":"d31a81ac.459b1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":400,"y":550,"wires":[]},{"id":"1e1c1edd.c8c591","type":"trigger","z":"d31a81ac.459b1","name":"","op1":"","op2":"Process not found","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":620,"y":470,"wires":[["7fd7c9e5.ca01e8"]]},{"id":"936f7d79.a90b9","type":"switch","z":"d31a81ac.459b1","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"uedit32.exe","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":470,"wires":[["1e1c1edd.c8c591","31ed49d3.b9f836"]]},{"id":"7fd7c9e5.ca01e8","type":"debug","z":"d31a81ac.459b1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":550,"wires":[]},{"id":"4606489c.2375c8","type":"mqtt-broker","name":"","broker":"192.168.0.233","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<div class="post-metadata">

**Author:** ![LuckyMooon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/luckymooon/32/80950_2.png) [@LuckyMooon](https://discourse.nodered.org/u/LuckyMooon)\
**Post date:** [29 June 2023 16:12 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/24 "2023-06-29T16:12:34Z")

</div>

Thank you again for this script.  
I tried to find out how to format all the processes you define as JASON, but that broke the script. Is there an easy way to format it to look like this?  
`Computer/Process= {"Process":"Discord.exe","Spotify.exe"}`

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [29 June 2023 17:20 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/25 "2023-06-29T17:20:50Z")

</div>

> [@LuckyMooon](#):
>
> I tried to find out how to format all the processes you define as JASON, but that broke the script. Is there an easy way to format it to look like this?

Yes, understood, but why??

Formating and iterating through a dictionary is all about Python stuff. You can do "almost" anything. The example I wrote gives a starting point, it can be expanded without limits and until end of life. I do not have that much time available for the moment to help further, maybe later. Try to think about how you would like to have it all, from the detection of the processes you are interested to watch and then the format of the data you would like to send to Node-RED

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [29 June 2023 18:57 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/26 "2023-06-29T18:57:05Z")

</div>

Just a quick update

In Python this is a dictionary:

```auto
to_watch = {"Process1":"Notepad.exe", "Process2":"uedit32.exe"}

```

to use a dictionary instead of a list, you will need to iterate through it in a different way, like this for example:

```auto
    for p in to_watch:
        if to_watch[p] in line:
            client.publish(topic, line)
            time.sleep(0.1)

```

Here is the complete updated script

```auto
#!/usr/bin/python
# Version adopted for Windows
# Usage: python process_checker.py

# import the necessary packages
import paho.mqtt.client as mqtt
import subprocess
import time

broker = "192.168.0.233"
topic = "processes"
#to_watch = ["Notepad.exe", "uedit32.exe"]
to_watch = {"Process1":"Notepad.exe", "Process2":"uedit32.exe"}

client = mqtt.Client()
resp = client.connect(broker, 1883, 60)

p = subprocess.Popen(['tasklist'], stdout=subprocess.PIPE)
out, err = p.communicate()

for line in out.splitlines():
    line = line.decode('windows-1252').strip('\x00')
    line = str(line).split(' ')[0]
    for p in to_watch:
        if to_watch[p] in line:
            client.publish(topic, line)
            time.sleep(0.1)

client.disconnect()
exit()

```

---

<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:** [28 August 2023 18:57 UTC](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439/27 "2023-08-28T18:57:11Z")

</div>

Closed on 28 August 2023 18:57 UTC

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

[Previous page](https://discourse.nodered.org/t/how-to-use-node-red-to-check-if-a-program-has-started/79439.md?page=1)
