在 Linux bash 脚本中杀死 10 分钟前的僵尸进程

发布于 2024-08-30 21:07:31 字数 384 浏览 1 评论 0原文

我一直在修改 yukondude 收效甚微。我正在尝试终止超过 10 分钟的进程。我已经知道进程 ID 是什么。我每 10 分钟循环一次数组,看看是否有任何挥之不去的进程存在并且需要被杀死。有人对此有什么快速的想法吗?

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

I've been tinkering with a regex answer by yukondude with 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-09-06 21:07:31

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

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


哦,你根本不是在谈论僵尸进程。这个 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

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

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.


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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文