I want a python script that terminate an aplications when I RUN another program.
Example: if I run vlc media player that will close kodi.exe
Or any other options to do this ?
It seems to me that what you have to do is enumerate processes by name, determine which to kill, and then kill it (the last part may be cumbersome a bit).
Did you have a chance to look at the following questions and their answers (they illustrate how to handle all of the steps above)?
Determining running programs in Python
List running processes on 64-bit Windows
Is it possible to kill a process on Windows from within Python?
Related
After I quit a running python program or notebook by Control + Z, there is still one or two python process background and occupy such high CPU resources.
Even after I run killall python or pkill python, It's still here and make my fans running fast;
python process
Please refer here: If we leave edge cases to one side, the difference is simple. Control+C aborts the application almost immediately while Control+Z shunts it into the background, suspended.
Therefore, you did not quit it, just suspended it. Use Control + C for quiting/terminating.
I have simple python code which is using 2 processes one is the main process and another which is created by multiprocessing module. Both processes runs in infinite loop. I want my python code to never crash/hang/freeze. I've already handled most of the errors/exceptions. FYI its a IOT project and I'm running this code as launcher in /etc/rc.local path. I tried using pid module from python as given here
Accoring to the link given the pid module works as below.
from pid import PidFile
with PidFile():
do_something()
My question is, does above logic meets my requirements or do I need to put some more logic like checking the existance of pid file and then decide to kill/stop/restart the process (or code itself) if any of the two processes freezes due to any bugs from code.
Please suggest is there any other way to achieve this, if pid module is not suitable for my requirement.
Hi I resolved this issue by creating a separate python scripts for both tasks rather using of multiprocessing modules such as queue. I suggest not to use multiprocessing queue inside infinite loops as it freezes the process/processes after some time.
I am making a python program, lets say A. Which is used to monitor python script B
When the python program shuts down, there is an exit function that as registered via atexit.register(), to do some clean up it need to re-run python script B, which need to stay running even when python script A has shutdown.
Python Script B can't be part of Python Script A.
What do I need to do to make that happen, I have already tried a few things like using subprocess.Popen(programBCommand), but that doesn't seem to work as it prevents A from shutting down.
I am using a Debian Operating System
If script B needs to be launched by script A, and continue running whether or not A completes (and not prevent A from exiting), you're looking at writing a UNIX daemon process. The easiest way to do this is to use the python-daemon module to make script B daemonize itself without a lot of explicit mucking about with the details of changing the working directory, detaching from the parent, etc.
Note: The process of daemonizing, UNIX-style, detaches from the process that launched it, so you couldn't directly monitor script B from script A through the Popen object (it would appear to exit immediately). You'd need to arrange some other form of tracking, e.g. identifying or communicating the pid of the daemonized process to script A by some indirect method.
I have several python.exe processes running on my Vista machine and I would like to kill one process thanks to the Windows task manager.
What is the best way to find which process to be killed. I've added the 'command line' column on task manager. It can help but not in all cases.
is there a better way?
Please consider replace Task Manager with the more powerful Process Explorer
here is a demo:
When I right clicked python in task manager and clicked open file location (win 10) it opened the plex installation folder so for me it is Plex that uses Python.exe to slow my computer down, shame as I use plex all the time for my Roku. Just have to put up with a slow computer and get another one.
I'm developing a script that runs a program with other scripts over and over for testing purposes.
How it currently works is I have one Python script which I launch. That script calls the program and loads the other scripts. It kills the program after 60 seconds to launch the program again with the next script.
For some scripts, 60 seconds is too long, so I was wondering if I am able to set a FLAG variable (not in the main script), such that when the script finishes, it sets FLAG, so the main script and read FLAG and kill the process?
Thanks for the help, my writing may be confusing, so please let me know if you cannot fully understand.
You could use atexit to write a small file (flag.txt) when script1.py exits. mainscript.py could regularly be checking for the existence of flag.txt and when it finds it, will kill program.exe and exit.
Edit:
I've set persistent environment variables using this, but I only use it for python-based installation scripts. Usually I'm pretty shy about messing with the registry. (this is for windows btw)
This seems like a perfect use case for sockets, in particular asyncore.
You cannot use environment variables in this way. As you have discovered it is not persistent after the setting application completes