I need to run a command adb get-state from my python library.
I used
subprocess.popen
to execute the command and killed the process using pid.
If the adb daemon is not in running state, adb process will create an extra adb process and terminate the first process created after starting the daemon (even manually its the same).
Hence I am not able to kill the adb process opened by subprocess open since it has the pid of the first adb process (which has already terminated)
Is there any way I can kill the real adb process which executes the command from python.
I will suggest you yo kill all the running processes of adb before starting a new one . Here is some sample code that can kill already running process under windows environment.
This code Kills any adb process that is already running and then opens a new process daemon for adb
Sample Code :
import subprocess
import os
p = subprocess.Popen("tasklist", stdout=subprocess.PIPE)
RunningProcessLIst = p.stdout.readlines()
print("Kill all running instances of adb.exe")
for runningPro in RunningProcessLIst :
if "adb.exe" in str(runningPro) :
os.system("taskkill /F /IM adb.exe")
process =subprocess.Popen("I:\\adb\\Win32\\adb.exe adb get-state") # you subprocesses call will be here
Hope this helps
Related
This question already has answers here:
How to kill all subprocesses of shell?
(9 answers)
Closed 2 years ago.
I have a shell script which in turn starts a python script. Python script further creates a process pool and does its stuff.
conda activate app_env
... some other shell commands ...
clean() {
# make sure all process created by python and python itself
# gets killed
}
trap clean SIGINT
python start_server.py
When someone manually tries to exit the script using ctrl+c, I need to make sure that all the processes started by python get killed. Is there a way where I could all the child process started by the shell script in one go or may be step by step?
Right now, I listen for the ctrl+c signal and then execute the cleaning function. After I have detected the ctrl+c signal, how do I kill the processes started by python i.e started by this shell script
If killing the python script would kill the processes you could use a trick with GNU screen. Start the python process as:
screen -S session_name -d -m your_python_process
and kill it in clean() with:
screen -X -S session_name quit
If this is not an option because killing the main python script would not kill all processes then maybe you should have a look at this SO question: How to kill zombie process or this one: killing Parent process along with child process using SIGKILL
I daemonized a python script using the daemonize python library, but now I cannot find the daemon that it spawned. I want to find the daemon and kill it to make some changes to the script.
I used the following to daemonize:
pidfile='/tmp/filename.pid'
daemon = Daemonize(app='filename',pid=pidfile, action=main)
print("daemon started")
daemon.start()
Open a terminal window and try the following:
ps ax | grep <ScriptThatStartedTheDaemon>.py
It should return the PID and the name of the process. Once you have the PID, do:
kill <pid>
Depending on how many times you've run your script, you may have multiple daemons running, in which case you'd want to kill all of them.
To make sure the process was terminated, run the first line of code again. The process with the PID that you killed shouldn't show up if it was successfully terminated.
I have a python script which launches a sequence of subprocesses in a loop:
for c in cmds:
subprocess.call(c, shell=True)
When the script is running in terminal, I try to stop it with Ctrl+C or Ctrl+D, however, it will continue to launch next subprocess. How can I terminate the whole python script at once?
What likely happens is that ctrl-C is intercepted by you sub-process (running on the same terminal) and never gets to your Python script. What you can do is to check the return value of subprocess.call to see whether the child process was killed by a signal and decide whether you want to stop submitting new processes.
I want to run a shell script from python.
The shell script is something which runs a server, which needs ctrl+c to break.
How to do that is there a way to run such type of scripts from python.
Just send the SIGINT signal to your app:
proc = subprocess.Popen(stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# When it needs to be stopped
proc.send_signal(signal.SIGINT)
How to get the PID of the processes started by the fabric run command.
I want to keep track of the PID, in case I want to kill the process.
Any better way of dealing with this case?
This should work because the shell opened by the run command is still open when the echo portion of the command is run:
run("mycommand & echo $!")