Unix 脚本在多次检查条件后终止进程并定期运行

发布于 2024-11-17 17:31:27 字数 651 浏览 6 评论 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”那么就没有必要被杀死。

I want to write a unix shell script to run a command 3 times in every 80 seconds and write the every sequence in a different line in a text file. And also if the all results are 10 or more in a line I want to kill the process:

for example:

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   

also

the output file:

5 5 5 

10 10 10 

Note the if the output sequence is "10 10 11", "10 11 12" etc. then the process should be killed again. But if it is like "9 9 10" then no need to be killed.

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

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

发布评论

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

评论(1

蓝天白云 2024-11-24 17:31:27

您想要实现什么目标?

听起来像是一种极其老套的监控进程的方法。
难道你不能简单地使用:

ulimit -T 10    # the maximum number of threads

或变体(man bash/ulimitEnter)吗?

这样,程序甚至可以更优雅地关闭自身。


注意:既然您建议使用 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

What are you trying to achieve?

Sounds like an extremely hacky way to monitor a process.
Couldn't you simply employ:

ulimit -T 10    # the maximum number of threads

or a variation (man bash , /ulimitEnter)?

That way a program could even possibly be more graceful in shutting itself down.


Note: since you suggest using kill -9 without trying other signals, perhaps you imply that signals never get handled? In that case you can probably use ulimit -i (the maximum number of pending signals)

Snippet

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