在 linux bash 脚本中杀死一个 10 分钟旧的僵尸进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2762233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:02:46  来源:igfitidea点击:

Kill a 10 minute old zombie process in linux bash script

linuxbashprocesskillzombie-process

提问by Steve

I've been tinkering with a regex answer by yukondudewith little success. I'm trying to kill processes that are older than 10 minutes. I already know what the process IDs are. I'm looping over an array every 10 min to see if any lingering procs are around and need to be killed. Anybody have any quick thoughts on this?

我一直在修补yukondude的正则表达式答案,但收效甚微。我试图杀死超过 10 分钟的进程。我已经知道进程 ID 是什么。我每 10 分钟循环一次数组,以查看是否有任何挥之不去的 proc 需要被杀死。有人对此有什么快速的想法吗?

ps -eo uid,pid,etime 3233332 | egrep ' ([0-9]+-)?([0-9]{2}:?){3}' | awk '{print }' | xargs -I{} kill {}

回答by caf

Just like real Zombies, Zombie processes can't be killed - they're already dead.

就像真正的僵尸一样,僵尸进程不能被杀死——它们已经死了。

They will go away when their parent process calls wait()to get their exit code, or when their parent process exits.

当它们的父进程调用wait()以获取它们的退出代码时,或者它们的父进程退出时,它们就会消失。



Oh, you're not really talking about Zombie processes at all. This bash script should be along the lines of what you're after:

哦,您根本就不是在谈论 Zombie 进程。这个 bash 脚本应该与你所追求的一致:

ps -eo uid,pid,lstart |
    tail -n+2 |
    while read PROC_UID PROC_PID PROC_LSTART; do
        SECONDS=$[$(date +%s) - $(date -d"$PROC_LSTART" +%s)]
        if [ $PROC_UID -eq 1000 -a $SECONDS -gt 600 ]; then
            echo $PROC_PID
        fi
     done |
     xargs kill

That will kill all processes owned by UID 1000 that have been running longer than 10 minutes (600 seconds). You probably want to filter it out to just the PIDs you're interested in - perhaps by parent process ID or similar? Anyway, that should be something to go on.

这将杀死 UID 1000 拥有的所有运行时间超过 10 分钟(600 秒)的进程。您可能只想将其过滤为您感兴趣的 PID - 可能是通过父进程 ID 或类似的?无论如何,这应该是继续下去的事情。