首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >不包括ps中的子进程

不包括ps中的子进程
EN

Server Fault用户
提问于 2012-03-20 02:59:49
回答 3查看 5.1K关注 0票数 2

背景:要重新加载应用程序配置,我需要kill -HUP父进程的PID。要查找PID,我目前使用的是ps auxf | grep gunicorn,输出示例如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ ps auxf | grep gunicorn
stpe      4222  0.0  0.2  64524 11668 pts/2    S+   11:01   0:00  |   \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4225  0.0  0.4  76920 16332 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4226  0.0  0.4  76932 16340 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4227  0.0  0.4  76940 16344 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4228  0.0  0.4  76948 16344 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4229  0.0  0.4  76960 16356 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4230  0.0  0.4  76972 16368 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4231  0.0  0.4  78856 18644 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4232  0.0  0.4  76992 16376 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      5685  0.0  0.0  22076   908 pts/1    S+   11:50   0:00  |   \_ grep --color=auto gunicorn
stpe      5012  0.0  0.2  64512 11656 pts/3    S+   11:22   0:00      \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5021  0.0  0.4  77656 17156 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5022  0.0  0.4  77664 17156 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5023  0.0  0.4  77672 17164 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5024  0.0  0.4  77684 17196 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5025  0.0  0.4  77692 17200 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5026  0.0  0.4  77700 17208 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5027  0.0  0.4  77712 17220 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5028  0.0  0.4  77720 17220 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py

基于以上所述,我发现它是4222和5012,我需要HUP。

问:我如何排除子进程而只获得父进程(但是请注意,我想要的进程也有我不感兴趣的父进程(例如bash) )?

对ascii树中有多少缩进的grep使用regexp感觉很脏。有更好的办法吗?

例如:所需的输出将类似于以下内容。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
stpe      4222  0.0  0.2  64524 11668 pts/2    S+   11:01   0:00  |   \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      5012  0.0  0.2  64512 11656 pts/3    S+   11:22   0:00      \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py

这很容易被解析,能够在执行HUPing的脚本中自动找到PID,这就是目标。

当然,从一开始,这可能不是解决这个问题的方法。

EN

回答 3

Server Fault用户

回答已采纳

发布于 2012-03-20 04:10:02

在我看来,用ps输出的regexp解析某些东西听起来不太干净。

典型的方法是在/var/run/gunicorn.pid下使用PID文件,然后只使用kill -HUP $(cat /var/run/gunicorn.pid)

如果这对你来说是不可能的,那么你需要再深入一点。

ppid参数在ps中显示子进程的父pid。所以就像

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ps -C "/usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py" -o ppid=

应该返回父进程id。

如果真的起作用了,那就照做

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ps -C "/usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py" -o ppid= | xargs kill -HUP
票数 3
EN

Server Fault用户

发布于 2012-03-20 04:15:31

在处理ps输出和grep以获取kill时,您的第一反应应该是“已经有人想过如何通过使用pgrep选项来实现这一点”。

pgrep -P 1 -f gunicorn

这将匹配整个命令行中的"gunicorn“进程(如果进程实际上是"python"),其中父PID为1,即启动为守护进程。

然后,如果您对结果感到满意,可以以类似的方式发出“杀死”:

pkill -P 1 -f gunicorn

但是,尽管如此,Janne的回答是“更好的”:您应该将gunicorn进程的PID记录在一个文件中,然后使用该文件的内容来管理该过程。我不知道gunicorn,但是在Ruby方面,您可以在unicorn.rb文件中指定一个pidfile。

票数 1
EN

Server Fault用户

发布于 2012-03-20 03:18:41

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
grep -vP '(\s{7}| grep)'


cat ttt
stpe      4222  0.0  0.2  64524 11668 pts/2    S+   11:01   0:00  |   \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4225  0.0  0.4  76920 16332 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4226  0.0  0.4  76932 16340 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4227  0.0  0.4  76940 16344 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4228  0.0  0.4  76948 16344 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4229  0.0  0.4  76960 16356 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4230  0.0  0.4  76972 16368 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4231  0.0  0.4  78856 18644 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      4232  0.0  0.4  76992 16376 pts/2    S+   11:01   0:00  |       \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      5685  0.0  0.0  22076   908 pts/1    S+   11:50   0:00  |   \_ grep --color=auto gunicorn
stpe      5012  0.0  0.2  64512 11656 pts/3    S+   11:22   0:00      \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5021  0.0  0.4  77656 17156 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5022  0.0  0.4  77664 17156 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5023  0.0  0.4  77672 17164 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5024  0.0  0.4  77684 17196 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5025  0.0  0.4  77692 17200 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5026  0.0  0.4  77700 17208 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5027  0.0  0.4  77712 17220 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
stpe      5028  0.0  0.4  77720 17220 pts/3    S+   11:22   0:00          \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py


$ cat ttt | grep -vP '(\s{7}| grep)'
stpe      4222  0.0  0.2  64524 11668 pts/2    S+   11:01   0:00  |   \_ /usr/bin/python /usr/local/bin/gunicorn app_api:app -c app_api.ini.py
stpe      5012  0.0  0.2  64512 11656 pts/3    S+   11:22   0:00      \_ /usr/bin/python /usr/local/bin/gunicorn app_game_api:app -c app_game_api.ini.py
票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/371492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文