在 Debian(以及大多数 Linux 发行版)上,“批量管理进程”通常可以从查看/筛选、统一信号控制、服务化、自动化调度、容器化几个层面来做。下面按常见场景给你一套可落地的方法。
ps + grep / pgrepps -ef | grep nginx
pgrep -a nginx
pgrep 可以直接拿到 PID 列表,方便后续批量操作top / htophtop
htop 支持筛选、批量选进程、发送信号(F9)pkill -f my_worker
# 或
kill $(pgrep -f my_worker)
for pid in $(pgrep -f my_worker); do
kill -HUP $pid
done
# 杀掉内存占用超过 500MB 的进程(谨慎)
ps -eo pid,rss,comm | awk '$2>512000 {print $1}' | xargs kill
如果你管理的是服务类进程,最好统一用 systemd。
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl list-units --type=service
# 批量重启所有 myapp 服务
systemctl restart 'myapp-*'
# /etc/systemd/system/myapp@.service
[Service]
ExecStart=/opt/myapp/bin/run %i
systemctl start myapp@1
systemctl start myapp@2
systemctl restart myapp@*
适合大量小脚本 / worker:
apt install supervisor
[program:worker]
command=python worker.py
numprocs=4
process_name=%(program_name)s_%(process_num)02d
autorestart=true
supervisorctl restart all
supervisorctl status
*/5 * * * * pkill -f expire_job; /opt/run_expire.sh
systemctl enable mybatch.timer
如果是几十上百个进程:
docker ps
docker restart $(docker ps -q)
| 场景 | 推荐方式 |
|---|---|
| 临时批量杀进程 | pkill / htop |
| 服务进程 | systemd |
| 多 worker | systemd @实例 或 supervisor |
| 定时任务 | cron / systemd timer |
| 大规模 | Docker / K8s |
如果你能说一下具体场景(比如:Web 服务?爬虫?队列 worker?临时任务?),我可以直接给你一套可复制的配置示例。