- 1 year ago
Automatically close a program if another program starts
To automatically close a program if another program starts, you can use a scripting language like Python to monitor the running processes and terminate the target program when the other program is detected. Here's an example using Python and the psutil
library:
pythonimport psutil
import time
def is_process_running(process_name):
for proc in psutil.process_iter(['name']):
if proc.info['name'] == process_name:
return True
return False
def close_program(program_name):
for proc in psutil.process_iter():
if proc.name().lower() == program_name.lower():
proc.terminate()
break
# Configuration
target_program = "target.exe"
other_program = "other.exe"
# Main loop
while True:
if is_process_running(other_program):
close_program(target_program)
time.sleep(1) # Check every 1 second
In this example, the is_process_running
function checks if a given process name is currently running. The close_program
function terminates the target program if it is detected.
You need to replace "target.exe"
with the name of the program you want to close, and "other.exe"
with the name of the program whose start will trigger the closing action. Adjust the time.sleep
interval as needed for the desired frequency of checking.
Save the code to a Python script file (e.g., monitor.py
) and run it using Python. The script will continuously monitor the running processes and close the target program whenever the other program is detected.
Note: Running the script will actively monitor the system processes and terminate the target program when necessary. Be cautious and ensure that the target and other programs are correctly specified to avoid accidentally closing unintended processes.