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

发布于 2024-12-07 22:36:49 字数 436 浏览 0 评论 0原文

我有一台 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 技术交流群。

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

发布评论

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

评论(2

眉目亦如画i 2024-12-14 22:36:49

根据 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 脚本

#!/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 -l code> 或 root 的 sudo crontab -l <​​/code> 将显示当前的 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 分钟运行一次脚本。这个可以配置一些。祝你好运。

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

#!/bin/bash

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

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 or sudo crontab -l for root will display the current cron file..

it should look something like this

# 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

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.

寄居人 2024-12-14 22:36:49

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

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

可以为您提供使用超过 10% CPU 的所有 ffmpeg 进程的 PID。

-o

I don't know if it's the best technology/language, but awk would work, e.g.

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

would give you the PIDs of all ffmpeg processes using more than 10% CPU.

-o

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