gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 11:16:06 27 4
gpt4 key购买 nike

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

在 CLI 中,我可以使用“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 也会打印大于 10.0 的 PID。看起来 ffmpeg 的工作进程在 20% 左右?你不想杀死那些。我的看起来在 10-15% 之间,但可以很容易地精炼更多。你注意到awk将不会将 kill -9 $1 打印到标准输出,但是,通过 bash 管道,这些调用将是“热的”。

我不熟悉 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 脚本

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

关于linux - 在 Ubuntu 上重生挂起进程的最佳语言/方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661986/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com