bash 如何查找/杀死特定的python程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16798111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:31:46  来源:igfitidea点击:

How to find/kill a specific python program

linuxbashpsgrep

提问by Chris

There are two different python programs running in this VM

此 VM 中运行着两个不同的 Python 程序

one is a background job who monitors a folder and then 'does stuff' (with several workers)

一个是监控文件夹然后“做事”的后台工作(有几个工人)

10835 ?        Sl     0:03 python main.py
10844 ?        Sl    34:02 python main.py
10845 ?        S     33:43 python main.py

the second one is started via script

第二个是通过脚本启动的

20056 pts/1    S+     0:00 /bin/bash ./exp.sh
20069 pts/1    S+     0:00 /bin/bash ./exp.sh
20087 pts/1    S+     0:10 python /home/path/second.py

i have tried numerous things to find a way to kill only the main program (i want to build a cron watchdog), but non succeded

我尝试了很多方法来找到只杀死主程序的方法(我想构建一个 cron 看门狗),但没有成功

first one i want to find only the hanging 'python main.py' process (accompanied by [defunct]), but i cant even find just this process alone.

第一个我只想找到挂起的“python main.py”进程(伴随着 [defunct]),但我什至无法单独找到这个进程。

the upper ones are from ps -ax (so they both are running currently) pgrep 'python' returns all PIDs, also from second.py which i dont want: (not usefull, so is pkill therefore)

上面的来自 ps -ax(所以它们当前都在运行) pgrep 'python' 返回所有 PID,也来自我不想要的 second.py :(没有用,因此 pkill 也是如此)

pgrep 'python'
10835
10844
10845
20087

pgrep 'python main.py' always returns empty, so does pgrep 'main.py;

pgrep 'python main.py' 总是返回空, pgrep 'main.py 也是如此;

the only thing which works

唯一有效的东西

ps ax | grep 'python main.py'

but this one also returns its own PID, grepping 'ps' isn't a prefered solution afair. when main.py hangs, it shows "python main.py [defunct]". a

但是这个也返回它自己的PID,grepping 'ps' 不是一个首选的解决方案。当 main.py 挂起时,它显示“python main.py [defunct]”。一种

ps ax | grep 'python main.py [defunct]'

is useless as test as it always returns true. pgrep for anything more than 'python' also returns always false. i am a bit clueless.

作为测试没用,因为它总是返回真。除了 'python' 以外的任何东西的 pgrep 也总是返回 false。我有点不知所措。

采纳答案by Pavel Strakhov

In your daemon python script you should create PID file:

在您的守护进程 python 脚本中,您应该创建 PID 文件:

def writePidFile():
  pid = str(os.getpid())
  f = open('/tmp/my_pid', 'w')
  f.write(pid)
  f.close()

Now killing this process is simple:

现在杀死这个进程很简单:

kill `cat /tmp/my_pid`

Or you can just use grepand filter its own process:

或者你可以使用grep和过滤它自己的过程:

ps ax | grep 'python main.py [defunct]' | grep -v grep

回答by irbanana

This works for me. Found it on the pgrepbro pages.

这对我有用。在pgrep兄弟页面上找到它。

Find the pids of processes with 'test.py' as an argument, like 'python test.py'

以“test.py”为参数查找进程的pid,例如“python test.py”

pgrep -f test.py

And I use it to check if a python process is running:

我用它来检查 python 进程是否正在运行:

searcher="backend/webapi.py"

if pgrep -f "$searcher" > /dev/null
then
    echo "$(date +%y-%m-%d-%H-%M-%S) $searcher is alive. Doing nothing."
else
    echo "No $searcher. Kickstarting..."
    pushd $HOME/there/;
    ./run_backend
    popd;
    echo "Pgrepping $searcher:"
    pgrep "$searcher" # out to loggers
fi

回答by Hristiyan Andreev

I think I've found a pretty safe way to do find the proper python process PID for a specific file only. The idea is to find all the python processes, then all "test.py" processes and intersect the results to find the correct PID.

我想我已经找到了一种非常安全的方法来为特定文件找到正确的 python 进程 PID。这个想法是找到所有的python进程,然后所有“test.py”进程并将结果相交以找到正确的PID。

I use the subprocess module in a python script (you can interpolate for .sh script):

我在 python 脚本中使用 subprocess 模块(您可以插入 .sh 脚本):

import subprocess as subp
#Get a list of all processes with a certain name
def get_pid(name):
    return list(map(int,subp.check_output(["pidof", "-c", name]).split()))
#Get a list of all pids for python3 processes
python_pids = get_pid('python3')
#Get a list of all pids for processes with "main.py" argument
main_py_pids = list(map(int,subp.check_output(["pgrep", "-f", "main.py"]).split()))
python_main_py_pid = set(python_pids).intersection(main_py_pids)
print(python_pids)
print(main_py_pids)
print(python_main_py_pid)

Result with 3 running python processes (including the one from this script) and one "sudo nano main.py" process:

结果有 3 个正在运行的 python 进程(包括这个脚本中的一个)和一个“sudo nano main.py”进程:

Python pids: [3032, 2329, 1698]
main.py pids: [1698, 3031]
Interesection: {1698}