在 Linux 中,使用 ps -ef | grep配合 awk来批量结束多个进程是一项高效的系统管理操作。其核心思路是:通过 grep筛选出目标进程,用 awk提取进程号(PID),最后将这些 PID 传递给 kill命令。
例如,我在使用nohup命令在后台运行了多个进程,如下:
nohup python -m http.server 8080 > /dev/null 2>&1 &
nohup python -m http.server 8081 > /dev/null 2>&1 &
nohup python -m http.server 8082 > /dev/null 2>&1 &
nohup python -m http.server 8083 > /dev/null 2>&1 &
nohup python -m http.server 8084 > /dev/null 2>&1 &
nohup python -m http.server 8085 > /dev/null 2>&1 &
nohup python -m http.server 8086 > /dev/null 2>&1 &
nohup python -m http.server 8087 > /dev/null 2>&1 &
nohup python -m http.server 8088 > /dev/null 2>&1 &
nohup python -m http.server 8089 > /dev/null 2>&1 &
使用命令查看:
如果我要杀掉这些进程,那么就需要一个一个杀掉很耗费时间,我们可以使用ps -ef配合awk,一次性杀掉所有进程:
# ps -ef | grep python | grep -v grep | awk '{print $2}' | xargs kill -9
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容