3 ways to efficiently kill all python processes under Linux (including scripts.sh)

created at 07-24-2021 views: 4

3 ways to kill all python processes under linux

This article mainly introduces 3 ways to kill all python processes under linux, friends in need can refer to.

check processes

In Linux system management, we sometimes need to kill all python processes. Beginners usually first check out the running process of python:

ps -ef | grep python 

and then kill them one by one, or write a script (Method 3 ), in fact, there are ready-made methods, here are 3 methods.

1 killall method

killall python

2 pkill way

sudo pkill python

3 ps method (script)

ps lists the pid of ttlsa, and then kills them one by one, which is cumbersome.

# ps -ef | grep python | grep -v grep | awk'{print $2}' | xargs kill -9
#!/bin/bash

PROCESS=`ps -ef | grep python | grep -v grep | awk '{print $2}' | xargs kill -9`
#PROCESSE=`ps -ef | grep demo1.py | grep -v grep | awk '{print $2}' | xargs kill -9`
#PROCESSA=`ps -ef | grep demo2.py | grep -v grep | awk '{print $2}' | xargs kill -9`
#PROCESSB=`ps -ef | grep demo3.py | grep -v grep | awk '{print $2}' | xargs kill -9`

4 ps method 2

ps -ef|grep python|grep -v grep|cut -c 9-15|xargs kill -15
  • | : the pipe character | is used to separate two commands, and the output of the command on the left side of the pipe character will be used as the input of the command on the right side of the pipe character.
  • ps aux is a command to view all processes in linux. At this time, the retrieved process will be used as the input of the next command grep python.
  • grep python: the output of grep python is that all processes that contain the keyword "python" are python programs.
  • grep -v grep: removes the processes containing the keyword grep from the listed processes.
  • cut -c 9-15: is to intercept the 9th to 15th characters of the input line, which happens to be the process number PID.
  • xargs: the xargs command in xargs kill -15 is used to take the output (PID) of the previous command as the parameter of the kill -15 command and execute the command.
  • kill -15 will exit the specified process normally, and -9 will forcibly kill
created at:07-24-2021
edited at: 07-24-2021: