3

Linux Shell中结束指定名称的进程

 2 years ago
source link: https://vps.la/2021/09/10/linux-shell%e4%b8%ad%e7%bb%93%e6%9d%9f%e6%8c%87%e5%ae%9a%e5%90%8d%e7%a7%b0%e7%9a%84%e8%bf%9b%e7%a8%8b/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

若发行版包含pgrep工具则可讲命令简化为,经验值Debain 10,  Ubuntu 20.04可用

kill -9 pgrep app


需求#

运行脚本调用其他程序有人值守可以用^C终止运行

若需自动化则要借助kill -9 [pid]终止进程,例如我们需要终止名称中含有app的进程

用法#

ps -ef | grep app | grep -v grep | cut -c 9-15 | xargs kill -9

解析#

ps -ef |将列出所有进程详细信息,传入下级管道
grep app |从收到的信息中过滤含有app的行,传入下级管道
grep -v grep |从收到信息中反向过滤含有grep的行(即grep进程本身),传入下级管道
cut -c 9-15 |从收到信息中裁剪第5-15列,传入下级管道
xargs kill -9结束进程(对于无法直接用管道的命令可利用xargs中转,传入参数)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK