Unix 脚本在多次检查条件后终止进程并定期运行
我想编写一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要实现什么目标?
听起来像是一种极其老套的监控进程的方法。
难道你不能简单地使用:
或变体(
man bash
,/ulimitEnter)吗?这样,程序甚至可以更优雅地关闭自身。
注意:既然您建议使用
kill -9
而不尝试其他信号,也许您暗示信号永远不会被处理? 您可以使用ulimit -i
(挂起信号的最大数量)在这种情况下,
What are you trying to achieve?
Sounds like an extremely hacky way to monitor a process.
Couldn't you simply employ:
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 useulimit -i
(the maximum number of pending signals)Snippet