如何仅获取进程的父 PID 并使用 grep 排除与其链接的子进程

How to get only the parent PID of a process and exclude child processes linked to it with grep

我正在编写一个可以杀死 PID 的 ansible 剧本。

我找到了一个与我相关的好答案:

但是,在我的例子中,第一步是 Get running processes,我有两个过程如下:

# when i do: ps -ef | grep appp.py

ubuntu   17765     1  2 12:14 pts/0    00:00:04 python appp.py 
ubuntu   17784 17765  4 12:15 pts/0    00:00:05 /home/ubuntu/venvs/myvnv/bin/python /home/ubuntu/deploy/appp.py 
ubuntu   17844 14784  0 12:17 pts/0    00:00:00 grep --color=auto appp.py

它杀死了 17765 然后失败了:

failed: [10.10.1.1] (item=17784) => {"ansible_loop_var": "item", "changed": true, "cmd": "kill 17784", "delta": "0:00:00.002196", "end": "2020-04-26 12:23:22.833284", "item": "17784", "msg": "non-zero return code", "rc": 1, "start": "2020-04-26 12:23:22.831088", "stderr": "/bin/sh: 1: kill: No such process", "stderr_lines": ["/bin/sh: 1: kill: No such process"], "stdout": "", "stdout_lines": []}

意思是……它找不到17784来杀死。

Question is: How do I get only the parent PID (17784) with ps grep and then pass it on to kill it?

拖到最后 | grep -v grep