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



我有一个Ubuntu Oneiric服务器,它运行几个ffmpeg实例(每个实例转码一个实时视频提要)。有时会有一个ffmpeg实例挂起。我说的"挂"是指这个过程没有结束,它只是坐在那里什么也不做。我正在使用Upstart来自动重生崩溃的进程,它工作正常,但它不检测进程何时挂起。

在命令行中,我可以很容易地检测到哪个进程挂起使用"ps axo pid,pcpu,comm | grep ffmpeg"。对于未挂起的进程,pcpu值将> 200,但对于挂起的进程,它将是100(或非常接近它)。在这种情况下,我只需要杀死挂起的进程,Upstart跳进来并重新生成它。

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

谢谢。F

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

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

注意,我的条件表达式更精细了一点。user980473’s也会打印大于10.0的pid。看起来ffmpeg的工作流程在20%左右?你不会想消灭它们的。我的目标是在10-15%之间,但这可以很容易地进一步完善。你注意到awk将比kill -9 $1打印到stdout,但是,使用bash管道这些调用将是'hot'。

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

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

这个问题问你怎么做?坐在命令行前每隔5分钟就输入这些是不合理的。我就会在这里安排卧底工作。

将此文件保存为bash脚本

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

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

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

允许我通过调用ffmpegcheck

来调用它

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

应该是这样的

# 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分钟运行一次脚本。这可以配置一些。好运。

我不知道这是不是最好的技术/语言,但是awk可以工作,例如

$ ps axo pid,comm,pcpu | awk '/ffmpeg/ {if ($3 >= 10.0) print $1}'

将给出使用超过10% CPU的所有ffmpeg进程的pid。

- o

最新更新