Knowing which python process to kill on linux? - python

I've run a python script on the command line that can't be killed with ctrl-C (SIGINT).
$ ./bad_script.py
^CTraceback(most recent call last):
...
KeboardInterrupt
^C
^C
...
<I give up>
When I look for this python process on another command line I see many options:
$ pidof python
1111 2222 3333 4444 5555 6666 # Which one is bad_script.py?
I want to kill my bad_script.py process, not the innocents.
Note this is not a duplicate of other questions that are similar because I want to know which process to kill:
OSX Terminal: How to kill all processes with the same name
Kill a python process
Kill python process with pkill python

You have a number of options. For example you can run the following ps command to list all running programs and use grep:
ps aux | grep bad_script
or if you have access to the source code, you could print the process id inside the script, at the start of the program:
import os
print os.getpid()
or just press Ctrl-\ to kill it in a different way by sending the SIGQUIT signal.

Related

Is there a way to kill uvicorn cleanly?

Is there a way to kill uvicorn cleanly?
I.e., I can type ^C at it, if it is running in the foreground on a terminal. This causes the uvivorn process to die and all of the worker processes to be cleaned up. (I.e., they go away.)
On the other hand, if uvicorn is running in the background without a terminal, then I can't figure out a way to kill it cleanly. It seems to ignore SIGTERM, SIGINT, and SIGHUP. I can kill it with SIGKILL (i.e. -9), but then the worker processes remain alive, and I have to track all the worker processes down and kill them too. This is not ideal.
I am using uvicorn with CPython 3.7.4, uvivorn version 0.11.2, and FastAPI 0.46.0 on Red Hat Enterprise Linux Server 7.3 (Maipo).
That's because you're running uvicorn as your only server. uvicorn is not a process manager and, as so, it does not manage its workers life cycle. That's why they recommend running uvicorn using gunicorn+UvicornWorker for production.
That said, you can kill the spawned workers and trigger it's shutdown using the script below:
$ kill $(pgrep -P $uvicorn_pid)
The reason why this works but not the kill on the parent pid is because when you ^C something, the signal is transmitted throughout all of its spawned processes that are attached to the stdin.
lsof -i :8000
This will check processes using port :8000. If you are using different port for fastAPI then change the port number. I was using postman and python for fastAPI. So check process with python, then copy the PID usually 4-5 numbers.
Then run
kill -9 PID
Where PID is the PID number you copied
Your can try running below command
kill -9 $(ps -ef | grep uvicorn | awk '{print $2}')
or
Create and Alias with the command and keep using that command.
For example:
alias uvicornprocess="kill -9 $(ps -ef | grep uvicorn | awk '{print $2}')"
In my case uvicorn managed to spawn new processes while pgrep -P was killing old ones,
so I decided to kill the whole process group at once, just like ^C does:
PID="$(pgrep -f example:app)"
if [[ -n "$PID" ]]
then
PGID="$(ps --no-headers -p $PID -o pgid)"
kill -SIGINT -- -${PGID// /}
fi
Each line explained:
pgrep -f example:app gets the PID of the parent uvicorn ... example:app
[[ -n "$PID" ]] checks this PID is not empty, to avoid further steps when uvicorn is not running
ps --no-headers -p $PID -o pgid gets PGID (Process Group ID) this PID is part of
kill -SIGINT is similar to polite ^C (you may use kill -9 for non-polite instant kill)
-- means the next token is a positional argument, not a named option, even if it starts with -
-${PGID - negative value lets kill know it is PGID, not PID
${PGID// /} removes all spaces ps added to PGID to align a column

Kill a background process using command

I am trying to kill a process which I ran in back ground not I want to cancel it using a command line. Below is the command I am using but I am getting error "Operation not permitted".
ps -aef | grep gs_roach | grep -v grep | awk '{print $2}' | xargs kill -2
At first, 2 is SIGINT. SIGINT is the interrupt signal. The terminal sends it to the foreground process when the user presses ctrl-c.
Secondly, you do now own the process you are trying to kill. This is the reason for both ps output —which did not list process — and the sudo requirement to kill the process.
You can use ps -aux to list all processes including those you don't own. You can also use -p argument to show a specific process which best fit your need
If you need to kill a process;
try kill command with with -9 signal
if the following command does not work
sudo kill PROCESS_ID
try to use the following command:
sudo kill -9 PROCESS_ID
or try to use the following command:
sudo kill -s SIGKILL PROCESS_ID
--
Please vote my answer, if my solution help you. Because I need to unlock my account! Thanks!

Kill python interpeter in linux from the terminal

I want to kill python interpeter - The intention is that all the python files that are running in this moment will stop (without any informantion about this files).
obviously the processes should be closed.
Any idea as delete files in python or destroy the interpeter is ok :D (I am working with virtual machine).
I need it from the terminal because i write c code and i use linux commands...
Hope for help
pkill -9 python
should kill any running python process.
There's a rather crude way of doing this, but be careful because first, this relies on python interpreter process identifying themselves as python, and second, it has the concomitant effect of also killing any other processes identified by that name.
In short, you can kill all python interpreters by typing this into your shell (make sure you read the caveats above!):
ps aux | grep python | grep -v "grep python" | awk '{print $2}' | xargs kill -9
To break this down, this is how it works. The first bit, ps aux | grep python | grep -v "grep python", gets the list of all processes calling themselves python, with the grep -v making sure that the grep command you just ran isn't also included in the output. Next, we use awk to get the second column of the output, which has the process ID's. Finally, these processes are all (rather unceremoniously) killed by supplying each of them with kill -9.
pkill with script path
pkill -9 -f path/to/my_script.py
is a short and selective method that is more likely to only kill the interpreter running a given script.
See also: https://unix.stackexchange.com/questions/31107/linux-kill-process-based-on-arguments
You can try the killall command:
killall python
pgrep -f <your process name> | xargs kill -9
This will kill the your process service.
In my case it is
pgrep -f python | xargs kill -9
pgrep -f youAppFile.py | xargs kill -9
pgrep returns the PID of the specific file will only kill the specific application.
If you want to show the name of processes and kill them by the command of the kill, I recommended using this script to kill all python3 running process and set your ram memory free :
ps auxww | grep 'python3' | awk '{print $2}' | xargs kill -9
to kill python script while using ubuntu 20.04.2 intead of Ctrl + C just push together
Ctrl + D
I have seen the pkill command as the top answer. While that is all great, I still try to tread carefully (since, I might be risking my machine whilst killing processes) and follow the below approach:
First list all the python processes using:
$ ps -ef | grep python
Just to have a look at what root user processes were running beforehand and to cross-check later, if they are still running (after I'm done! :D)
then using pgrep as :
$ pgrep -u <username> python -d ' ' #this gets me all the python processes running for user username
# eg output:
11265 11457 11722 11723 11724 11725
And finally, I kill these processes by using the kill command after cross-checking with the output of ps -ef| ...
kill -9 PID1 PID2 PID3 ...
# example
kill -9 11265 11457 11722 11723 11724 11725
Also, we can cross check the root PIDs by using :
pgrep -u root python -d ' '
and verifying with the output from ps -ef| ...

Exit a Python process not kill it (via ssh)

I am starting my script locally via:
sudo python run.py remote
This script happens to also open a subprocess (if that matters)
webcam = subprocess.Popen('avconv -f video4linux2 -s 320x240 -r 20 -i /dev/video0 -an -metadata title="OfficeBot" -f flv rtmp://6f7528a4.fme.bambuser.com/b-fme/xxx', shell = True)
I want to know how to terminate this script when I SSH in.
I understand I can do:
sudo pkill -f "python run.py remote"
or use:
ps -f -C python
to find the process ID and kill it that way.
However none of these gracefully kill the process, I want to able to trigger the equilivent of CTRL/CMD C to register an exit command (I do lots of things on shutdown that aren't triggered when the process is simply killed).
Thank you!
You should use "signals" for it:
http://docs.python.org/2/library/signal.html
Example:
import signal, os
def handler(signum, frame):
print 'Signal handler called with signal', signum
signal.signal(signal.SIGINT, handler)
#do your stuff
then in terminal:
kill -INT $PID
or ctrl+c if your script is active in current shell
http://en.wikipedia.org/wiki/Unix_signal
also this might be useful:
How do you create a daemon in Python?
You can use signals for communicating with your process. If you want to emulate CTRL-C the signal is SIGINT (which you can raise by kill -INT and process id. You can also modify the behavior for SIGTERM which would make your program shut down cleanly under a broader range of circumstances.

Run console application in background

I am working on a script in python where first I set ettercap to ARP poisoning and then start urlsnarf to log the URLs. I want to have ettercap to start first and then, while poisoning, start urlsnarf. The problem is that these jobs must run at the same time and then urlsnarf show the output. So I thought it would be nice If I could run ettercap in background without waiting to exit and then run urlsnarf. I tried command nohup but at the time that urlsnarf had to show the url the script just ended. I run:
subprocess.call(["ettercap",
"-M ARP /192.168.1.254/ /192.168.1.66/ -p -T -q -i wlan0"])
But I get:
ettercap NG-0.7.4.2 copyright 2001-2005 ALoR & NaGA
MITM method ' ARP /192.168.1.254/ /192.168.1.66/ -p -T -q -i wlan0' not supported...
Which means that somehow the arguments were not passed correctly
You could use the subprocess module in the Python standard library to spawn ettercap as a separate process that will run simultaneously with the parent. Using the Popen class from subprocess you'll be able to spawn your ettercap process run your other processing and then kill the ettercap process when you are done. More info here: Python Subprocess Package
import shlex, subprocess
args = shlex.split("ettercap -M ARP /192.168.1.254/ /192.168.1.66/ -p -T -q -i wlan0")
ettercap = subprocess.Popen(args)
# program continues without waiting for ettercap process to finish.

Categories