- 1 year ago
How to check if a new process started in windows?
To check if a new process has started in Windows, you can use the psutil
library in Python. psutil
provides a cross-platform interface for retrieving information about running processes. Here's an example of how you can use psutil
to check if a new process has started:
pythonimport psutil
# Get the initial list of running process IDs
initial_process_ids = psutil.pids()
# Start the process that you want to monitor
# Wait for the new process to start
while True:
# Get the current list of running process IDs
current_process_ids = psutil.pids()
# Check for new process IDs
new_process_ids = set(current_process_ids) - set(initial_process_ids)
# If there are new process IDs, a new process has started
if new_process_ids:
print("New process started!")
# Optionally, you can retrieve information about the new process using psutil
# Break out of the loop
break
# Wait for a short period before checking again
time.sleep(1)
In this example, we first obtain the initial list of running process IDs using psutil.pids()
. Then, we enter a loop where we repeatedly check for new process IDs. The loop continues until a new process ID is detected. Once a new process ID is found, you can take appropriate actions, such as printing a message or retrieving further information about the new process using psutil
.
Make sure to install psutil
by running pip install psutil
before running the script.
Note that the code above continuously checks for new processes in a loop. You may need to adapt this approach based on your specific requirements and use case.