I want to achieve this.
I can run the process by os.system('PROCESS_NAME &')
But I don't know how to kill it.
And I don't wanna the external process blocking in my process
run external daemon process
do my jobs, do anything
kill the `running external daemon process`
On Linux OS, I wonder kill the task when meets some requirement
Related
First just for reference here is an answer to the question "How to terminate a python subprocess launched with shell=True"
In this the user is launching a subprocess and he wants to terminate it later. I have tried it, it works fine.
Now, I want to do something similar but in a remote machine. The remote machine can be accessed through ssh with no problem.
so I have
import os
import signal
import subprocess
import time
SERVER = "remote_server"
#Here initiate a subprocess that measures the cpu but this time remotely
pro= subprocess.Popen("ssh "+SERVER+ " sar -u 1 > mylog.log")
time.sleep(10)
#here kill the process (what should I put HERE??
#Kill the remote process
As you can see I initiate a process that runs sar -u 1 > mylog.log in a remote machine. This process will start running
After 10 secs, I want the remote process to stop. How do I kill it??
I think putting simply os.killpg(os.getpgid(pro.pid), signal.SIGTERM) would not kill it, would it?
All you have to do is proc.terminate() or proc.kill() (prefer the first one)
See https://docs.python.org/3/library/subprocess.html#subprocess.Popen.kill
Your remote program will be killed, because SSH automatically kills off processes that are no longer associated with an SSH session (which most people find out the hard way)
I'm developing a ADB client for python, i am planning to invoke adb binary with sub process to get the information.
Here is how i tried to invoke it, to start the adb server.
check_output([ 'adb.exe','start-server'],stderr=STDOUT)
I do see the adb running, but the program is getting stuck after that.
I have tried with shell=True, but that didn't affect it.
When i kill adb from task manager, the program does exit, and prints the right ouput.
How can i fix this, I assume that the command doesn't exit since the daemon is running ?
I was able to overcome this by starting the command in a separate thread, and using the current thread with other adb commands, as they return immediately.
Is there a more elegant solution ?
You can do that with subprocess.Popen.
import subprocess
adb = subprocess.Popen(['adb.exe', 'start-server'])
# Do some other stuff while adb is running...
adb.terminate() # Kill the process once you're done
This also has some advantages, like the possibility of giving input to the process through stdin, by using Popen.communicate()
Is there any way to kill a program that ignores all exceptions? Stupid, I know. I was testing something (since I wasn't sure what error a failed, embedded pig script would throw), forgot to limit the loop to a single day, and now it's just continuously running even though I used
ps -ef
to find and directly kill it. I would just let it run to completion since it will definitely terminate, but it runs hadoop jobs, and is needlessly using up resources/popping up on the terminal in between other tasks randomly. I'd like to avoid shutting my desktop down since I'm running other tasks, but will if it'll kill it...
I got the pid from
ps -ef
and used
kill -9
to directly kill it. It no longer shows up when I run
ps -ef | grep
but when I leave my terminal sitting for a little bit (even a new window) these "ghost" hadoop jobs show up that correspond to where the killed task would be.
Normally your Python program would need registered listeners to handle any kill signal you send it. See here:
https://stackoverflow.com/a/1112350/276949
There is a special kill signal (SIGKILL, denoted by-9) which will kill your process no matter what.
kill -9 <pid>
I'm obtaining a PID, using python, of a CGI script, however, the PID is not valid i.e. can't Taskkill it from CL. I get:
"Process: no process found with pid xxxx" where xxxx is the pid
I thought maybe I have to kill a parent python shell instance, but os.ppid doesn't work in windows.
So then I installed psutil python module and can now get parent PID, but it just shows the parent as the actual WebServer (Abyss), which I don't think I want to kill, since it is the http process that I notice runs constantly and not just a CGI interpreter instance.
Using psutil I CAN get the process status of the actual script, using the pid returned by os.getpid(), and see that it is running. So the pid works for purposes of process information retrieval using psutil. But this gets me no further to obtaining the actual PID I need to kill the script, using EITHER Taskkill on the CL or via kill() from psutil!
What exactly is a cgi shell script from a process perspective, and if it is not a process, why is os.getpid() returning a pid?
Why are you assured that your CGI script are still working when you try to kill it? Web server starts one instance of CGI script for one request and when script finishes it... just finishes.
I've got a Python script managing a gdb process on Windows, and I need to be able to send a SIGINT to the spawned process in order to halt the target process (managed by gdb)
It appears that there is only SIGTERM available in Win32, but clearly if I run gdb from the console and Ctrl+C, it thinks it's receiving a SIGINT. Is there a way I can fake this such that the functionality is available on all platforms?
(I am using the subprocess module, and python 2.5/2.6)
Windows doesn't have the unix signals IPC mechanism.
I would look at sending a CTRL-C to the gdb process.