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.
Ok, anyway, thank you very much for your effort and helpfulness
Yes, that is correct
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
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
#!/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()
[{"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":""}]
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"}
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
Just a quick update
In Python this is a dictionary:
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:
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
#!/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()
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.