0

我想编写一个 unix shell 脚本以每 80 秒运行 3 次命令,并将每个序列写入文本文件的不同行中。而且,如果所有结果在一行中为 10 或更多,我想终止该进程:

例如:

pstack <pid> | grep -c 'abcd'

5

pstack <pid> | grep -c 'abcd'

5

pstack <pid> | grep -c 'abcd'

5

//Nothing to do.

//after 80 seconds again it runs:

pstack <pid> | grep -c 'abcd'

10 

pstack <pid> | grep -c 'abcd'

10

pstack <pid> | grep -c 'abcd'

10

kill -9 < PID>     // because all three outputs are bigger than 10   

输出文件:

5 5 5 

10 10 10 

请注意,如果输出序列是“10 10 11”、“10 11 12”等,则应再次终止该进程。但是如果它像“9 9 10”那么就不需要被杀死。

4

1 回答 1

1

你想达到什么目的

听起来像是一种非常骇人听闻的监控流程的方法。你不能简单地雇用:

ulimit -T 10    # the maximum number of threads

或变体(man bash/ulimit Enter)?

这样一个程序甚至可以更优雅地关闭自己。


注意:既然您建议kill -9在不尝试其他信号的情况下使用,也许您暗示信号永远不会被处理?在这种情况下,您可能可以使用ulimit -i待处理信号的最大数量

片段

#!/bin/bash

function dumpstack()
{
    pstack $(pgrep a.exe) | grep -c abcd
}

while sleep 1; do dumpstack; done | tee rawoutput.log |
    {
        trap "" INT
        count=0;
        while read stackframes; do 
            if [[ $stackframes -lt 10 ]]; then
                count=0
            else
                count=$(($count+1))
            fi

            if [[ $count -ge 3 ]]; then
                echo KILL -9 !
                            break
            fi
            echo "(debug frames:$stackframes, count:$count)"
        done
    } | tee cooked_output.log
于 2011-06-27T14:40:20.807 回答