首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ubuntu上重生挂起进程的最佳语言/方法?

在Ubuntu上重生挂起进程的最佳语言/方法?
EN

Stack Overflow用户
提问于 2011-10-05 21:26:51
回答 2查看 735关注 0票数 3

我有一个Ubuntu Oneiric服务器,它运行几个ffmpeg实例(每个实例都转码一个实时视频提要)。不时会有一个ffmpeg实例挂起。所谓“挂起”,我的意思是这个过程并没有结束,它只是坐在那里什么也不做。我正在使用Upstart自动恢复崩溃的进程,它工作正常,但它不会检测到进程何时挂起。

在CLI中,我可以使用"ps axo pid,pcpu,comm | grep ffmpeg“轻松检测哪个进程挂起了。对于未挂起的进程,pcpu值将大于200,但对于挂起的进程,它将是100 (或非常接近它)。在这种情况下,我只需终止挂起的进程,Upstart就会加入并重新启动它。

我是Linux的新手,所以我的问题是:什么是自动化的最好的技术/语言?我想我需要做的是解析ps的输出,找到pcpu接近100的实例,然后杀死这些实例。

谢谢。

F

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-05 22:41:06

基于user980473的回答,我可能也会使用awk,但我会调用我的命令并通过管道将其传递给bash,而不是直接返回PID。不过,我会删除grep,只使用awk并将条件语句移动到大括号内。

ps axo pid,comm,pcpu| awk '/ffmpeg/ {if ($3 <= 15.0 && $3 >= 10.0) print "kill -9 "$1}' | bash

请注意,我的条件表达式稍微更精致一些。因为user980473也会打印大于10.0的PID。看来ffmpeg的工作过程在20%左右?你不会想杀了他们的。我的看起来在10-15%之间,但这很容易改进得更多。你会注意到awk会比打印kill -9 $1到stdout,但是,有了这个管道,这些调用将是“热的”。

我不熟悉upstart,但是你可以使用更多的命令。也许你需要调用一个本地的python脚本,然后这个命令看起来几乎是一样的,但是在$1之后,你会看到";./rebootScript.py“

ps axo pid,comm,pcpu| awk '/ffmpeg/ {if ($3 <= 15.0 && $3 >= 10.0) print "kill -9 "$"; ./rebootScript.py"}'

那么这个问题就是你会怎么做呢?坐在CLI前,每隔5分钟输入一次,这是不合理的。这就是我要设置cron作业的地方。

将此文件另存为bash脚本

代码语言:javascript
复制
#!/bin/bash

ps axo pid,comm,pcpu| awk '/ffmpeg/ {if ($3 <= 15.0 && $3 >= 10.0) print "kill -9 "$1}' | bash

接下来,设置正确的权限。sudo chmod +x ./ffmpegCheck.sh

并将脚本移动到您想要保存它的位置。我会把我的放在mv ffmpegCheck.sh /usr/local/bin/ffmpegcheck

这将允许我通过简单地调用ffmpegcheck来调用它

根用户的crontab -lsudo crontab -l将显示当前的cron文件。

它应该看起来像这样

代码语言:javascript
复制
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

您将需要向列表中添加一个条目。我可能会输入sudo crontab -e,但还有其他方法。并添加*/3 * * * * /usr/local/bin/ffmpegcheck # ffmpeg check

这将每3分钟运行一次脚本。这可以进行一些配置。祝好运。

票数 1
EN

Stack Overflow用户

发布于 2011-10-05 21:43:52

我不知道它是否是最好的技术/语言,但是awk可以工作。

代码语言:javascript
复制
$ ps axo pid,comm,pcpu | awk '/ffmpeg/ {if ($3 >= 10.0) print $1}'

会给你所有使用超过10% CPU的ffmpeg进程的PID。

-o

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7661986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档