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

I would like to use Node-Red to send MQTT messages from my PC when a program is started or closed. For example, if I open Discord, my PC should inform my smart home via Mqtt.
I've already researched but found nothing, is there an extension for this, or is there another way?
Thanks in advance.

There are several ways to monitor if a process is started or not but it might depend on what operating system we are talking about

Myself I use Python scripts to check. In Python you can use subprocess to check for running processes. In my scripts I then communicate the results back to Node-RED via MQTT

Monitoring a process or program and just checking if it is in a process list does not necessary say if it is still working, you just find it listed as a started process. When I write my own programs or scripts, I always include a heartbeat feature. This tells me a bit more, at least my program is able to process a heartbeat request and send the response back. Hopefully the rest of the program is then working as expected. Also for this purpose, MQTT is a perfect choice

Thanks for the quick reply.
I use node-red on windows. Would it be possible to show me an example script since I'm not very familiar with python?

Is that a program, or do you mean that you open a page in a browser? If in a browser I suspect it may not be possible to tell, as it will not open a specific discord process. Unless there is a browser plugin that can help.

If don't have that, you have a learning curve ahead. Give me some time and I will see if I can make a simple sample including MQTT so you can get the info into Node-RED as well (all of my Node-RED stuff and Python scripts are runnning under Linux so havn't tested mine under Windows)

First thing you need to do is to check if you have Python installed. in your Windows box. Otherwise you have to install that first. Open a command prompt and just type python, check & show what you get

Next you should read up a bit on Python

Maybe some other member will join this discussion with other alternative ideas?

Like this (not sure if it works with Windows):

If in a browser, I once wrote a program in C++ with Visual Studio with the browser functionlity. That gave me full control of everything including possibilities to check and parse content from the web server. Such a dedicated program would be possible to write also with MQTT etc but it is not a beginner task and would take some time -> means cost if you hire a consultant (if this is a commercial need)

I meant the Discord.exe program as an example. Or other programs running on the computer

I already installed Python and I tried to install the node-red-contrib-process-list extension, but I can't install any extensions via the web interface (I don't know why) and in the command prompt, installing this extension didn't work.

If that doesn't represent too much effort for you, such an example would be great.

If you see errors similar to those in this issue (which I have just submitted), it may be that it is not compatible with the version of nodejs you have. It may be something else, though, so we will have to wait for the author to respond.
Otherwise you will have to find an alternative route. It should not be difficult using an exec node to call a system command to give you the process names.
If your error is not similar to those in the issue then post the full output you see when you try to install from the command line.

I'm not quite sure if it's the same or a similar error message.

Here the error message:

npm WARN cleanup Failed to remove some directories [
npm WARN cleanup   [
npm WARN cleanup     'C:\\Users\\user\\node_modules\\are-we-there-yet',
npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'C:\Users\user\node_modules\are-we-there-yet\node_modules\readable-stream'] {
npm WARN cleanup       errno: -4048,
npm WARN cleanup       code: 'EPERM',
npm WARN cleanup       syscall: 'rmdir',
npm WARN cleanup       path: 'C:\\Users\\user\\node_modules\\are-we-there-yet\\node_modules\\readable-stream'
npm WARN cleanup     }
npm WARN cleanup   ]
npm WARN cleanup ]
npm ERR! code 1
npm ERR! path C:\Users\user\node_modules\process-list
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@9.4.0
npm ERR! gyp info using node@19.2.0 | win32 | x64
npm ERR! gyp info find Python using Python version 3.11.3 found at "C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS not looking for VS2015 as it is only supported up to Node.js 18
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:380:14)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2015 (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:364:14)
npm ERR! gyp ERR! stack     at C:\Users\levin\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:67:12
npm ERR! gyp ERR! stack     at failPowershell (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:156:7)
npm ERR! gyp ERR! stack     at VisualStudioFinder.parseData (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:170:14)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:143:14
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:411:7)
npm ERR! gyp ERR! System Windows_NT 10.0.22621
npm ERR! gyp ERR! command "C:\\Program Files\\SystemTools\\nodejs\\node.exe" "C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Users\user\node_modules\process-list
npm ERR! gyp ERR! node -v v19.2.0
npm ERR! gyp ERR! node-gyp -v v9.4.0
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in: C:\Users\user\AppData\Local\npm-cache\_logs\2023-06-28T11_04_17_734Z-debug-0.log

What folder did you run that command from? You must do it from your .node-red folder.
If that removes the permissions error, but you still get the visual studio error then that suggests that when you installed nodejs that you did not tell it to install the build tools. Also I see you have installed nodejs 19, which is not a supported version. Uninstall nodejs and install version 18.
See the node red getting started docs for how to install on windows.

I have now installed node.js 18 and tried again, but with the same result.

When I try to install other extensions I don't have to change the path in the cmd window, or is it only necessary with this extension? And where is this folder usually located?

npm WARN cleanup Failed to remove some directories [
npm WARN cleanup   [
npm WARN cleanup     'C:\\Users\\user\\node_modules\\are-we-there-yet',
npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'C:\Users\user\node_modules\are-we-there-yet\node_modules\readable-stream\doc'] {
npm WARN cleanup       errno: -4048,
npm WARN cleanup       code: 'EPERM',
npm WARN cleanup       syscall: 'rmdir',
npm WARN cleanup       path: 'C:\\Users\\user\\node_modules\\are-we-there-yet\\node_modules\\readable-stream\\doc'
npm WARN cleanup     }
npm WARN cleanup   ]
npm WARN cleanup ]
npm ERR! code 1
npm ERR! path C:\Users\user\node_modules\process-list
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@9.4.0
npm ERR! gyp info using node@18.16.1 | win32 | x64
npm ERR! gyp info find Python using Python version 3.11.3 found at "C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:380:14)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:401:16
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:427:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1091:16)
npm ERR! gyp ERR! System Windows_NT 10.0.22621
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Users\levin\node_modules\process-list
npm ERR! gyp ERR! node -v v18.16.1
npm ERR! gyp ERR! node-gyp -v v9.4.0
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in: C:\Users\user\AppData\Local\npm-cache\_logs\2023-06-28T12_23_33_984Z-debug-0.log

Can you post the start of that again please, including the command you are entering so that I can see which folder you are running it from.

I run this command:

C:\Users\user>npm install node-red-contrib-process-list

Did you not read my post? I said that you must go to your .node-red folder, where your flow file is located. Usually that is c:/Users/your user name/.node-red

Here is a working solution with a Python script. You can run it from a command prompt and the result is sent to Node-RED via MQTT (you have to change the broker ip to fit your broker)


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

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

broker = "192.168.0.233"
topic = "processes"

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)
    client.publish(topic, line)

client.disconnect()
exit()

I read your post very well, but only found the node_modules folder and wasn't sure if that was it. I checked again and found the right folder.

However, I still get an error message:

C:\Users\user\.node-red>npm install node-red-contrib-process-list
npm ERR! code 1
npm ERR! path C:\Users\user\.node-red\node_modules\process-list
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@9.4.0
npm ERR! gyp info using node@18.16.1 | win32 | x64
npm ERR! gyp info find Python using Python version 3.11.3 found at "C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:380:14)
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:401:16
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack     at C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:427:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1091:16)
npm ERR! gyp ERR! System Windows_NT 10.0.22621
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Users\user\.node-red\node_modules\process-list
npm ERR! gyp ERR! node -v v18.16.1
npm ERR! gyp ERR! node-gyp -v v9.4.0
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in: C:\Users\user\AppData\Local\npm-cache\_logs\2023-06-28T16_17_09_405Z-debug-0.log

So that has got rid of the permissions error.

How did you install nodejs 18 and did you see a checkbox to install the build tools, and did you select that?

First of all, thank you for your effort and work. The script worked.
Did I understand correctly that the script lists all processes and then sends them one after the other via MQTT?
If this is the case, is there a way to check just one or more specific processes, because at Mqtt Broker, only one random process arrives from those listed.
And my second question is how to automate the script so that it is constantly running, or at certain time intervals.

I downloaded the node-v18.16.1-x64.msi file from the website and after uninstalling node.js 19... i installed it.
Then I followed the installation instructions and ticked the "Install necessary tolls" option. Maybe the installation of it didn't work, I could try to install it separately.