在 Ubuntu 上重生挂起进程的最佳语言/方法?
我有一台 Ubuntu Oneiric 服务器,它运行多个 ffmpeg 实例(每个实例对实时视频源进行转码)。有时,其中一个 ffmpeg 实例会挂起。我所说的“挂起”是指该过程不会结束,它只是坐在那里什么也不做。我正在使用 Upstart 自动重新生成崩溃的进程,这可以正常工作,但它无法检测进程何时挂起。
在 CLI 中,我可以使用“ps axo pid,pcpu,comm | grep ffmpeg”轻松检测哪个进程已挂起。对于未挂起的进程,pcpu 值将 > 200,但对于挂起的来说,它将是 100(或非常接近)。在这种情况下,我只需终止挂起的进程,Upstart 就会跳入并重新生成它。
我对 Linux 还很陌生,所以我的问题是:实现自动化的最佳技术/语言是什么?我想我需要做的是解析 ps 的输出以找到 pcpu 接近 100 的实例,然后杀死这些实例。
谢谢。
F
I have an Ubuntu Oneiric server that runs several instances of ffmpeg (each one transcoding a live video feed). From time to time one of the ffmpeg instances will hang. By "hang" I mean the process doesn't end, it just sits there doing nothing. I'm using Upstart to automatically respawn processes that crash, which works OK, but it doesn't detect when a process has hung.
At the CLI I can easily detect which process has hung using "ps axo pid,pcpu,comm | grep ffmpeg". For processes that are not hung, the pcpu value will be > 200, but for a hung one it'll be 100 (or very close to it). In this scenario, I simply have to kill the hung process and Upstart jumps in and respawns it.
I'm fairly new to Linux, so my question is: what's the best technology / language to automate this? I guess what I need to do is parse the output from ps to find instances with pcpu near 100, and then kill those instances.
Thanks.
F
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 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 也会打印大于 10.0 的 PID。貌似ffmpeg的工作进程在20%左右?你不会想杀死那些。我的看起来在 10-15% 之间,但是可以很容易地进一步完善。您注意到 awk 会将kill -9 $1 打印到标准输出,但是使用 bash 管道,这些调用将是“热门”。
我不熟悉暴发户,但你可以更多命令。也许您需要随后调用本地 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 脚本
接下来,设置正确的权限。
sudo chmod +x ./ffmpegCheck.sh
并将脚本移动到您想要保留的位置。我将我的放在
mv ffmpegCheck.sh /usr/local/bin/ffmpegcheck
中,来调用它
这将允许我通过简单地调用
ffmpegcheck
crontab -l
code> 或 root 的 sudo crontab -l </code> 将显示当前的 cron 文件。它应该看起来像这样,
您将需要向列表中添加一个条目。我会输入 sudo crontab -e 但还有其他方法。
并添加
*/3 * * * * /usr/local/bin/ffmpegcheck # ffmpeg check
这将每 3 分钟运行一次脚本。这个可以配置一些。祝你好运。
buliding on user980473's answer I would probably use awk as well, but instead just returning the PID I would call my command and pipe it to bash. Although, I would remove grep and just use awk and move the conditional statement inside the braces.
ps axo pid,comm,pcpu| awk '/ffmpeg/ {if ($3 <= 15.0 && $3 >= 10.0) print "kill -9 "$1}' | bash
notice that my conditional expression is a bit more refined. as user980473's would also print the PIDs greater than 10.0. It appears the working processes of ffmpeg are around 20%? you wouldn't want to kill off those. mine looks at between 10-15%, but that can easily be refined more. You notice
awk
will than print kill -9 $1 to stdout but, with the pipe to bash these calls will be 'hot'.I am unfamiliar with upstart, but you can more commands. Perhaps you need to call a local python scripit afterwards the command would look virtually the same, but after the $1 you would have "; ./rebootScript.py"
or
ps axo pid,comm,pcpu| awk '/ffmpeg/ {if ($3 <= 15.0 && $3 >= 10.0) print "kill -9 "$"; ./rebootScript.py"}'
so this than asks how would you do this? Sitting at the CLI and typing this every 5mins is unresonable.
this is where I would set up a cron job.
save this file as a bash script
NEXT, set the correct permissions.
sudo chmod +x ./ffmpegCheck.sh
and move the script to where you would like to keep it. I would place mine in
mv ffmpegCheck.sh /usr/local/bin/ffmpegcheck
this would allow me to invoke it by simply calling
ffmpegcheck
crontab -l
orsudo crontab -l
for root will display the current cron file..it should look something like this
you will want to add an entry to the list. I would type
sudo crontab -e
but there are other methods.and add
*/3 * * * * /usr/local/bin/ffmpegcheck # ffmpeg check
this will run the script every 3 minutes. This can be configured some. Good Luck.
我不知道这是否是最好的技术/语言,但 awk 可以工作,例如
可以为您提供使用超过 10% CPU 的所有 ffmpeg 进程的 PID。
-o
I don't know if it's the best technology/language, but awk would work, e.g.
would give you the PIDs of all ffmpeg processes using more than 10% CPU.
-o