大网数据广告活动
服务器运维问答

use nethogs in a script

· 52CE 我爱测

nethogs is a great utility to monitor network traffic by process. However, it is "interactive" and not suitable to be used in script... How can I achieve the following using nethogs or similar tool: I want to monitor a process (b…

问题原文

原帖链接 · xrfang · CC BY-SA 4.0

nethogs is a great utility to monitor network traffic by process. However, it is "interactive" and not suitable to be used in script... How can I achieve the following using nethogs or similar tool:

I want to monitor a process (by its id or name) for a few second, get its traffic (as a number, in any unit such as KB/s), if the traffic is lower than a threshold, the script returns non-zero.

Thanks a lot.

EDIT:

  1. Debian 10 Server
  2. The program I need to monitor is ffmpeg, which push RTSP stream to remote RTMP server. i.e. it is a "client", not "server".
  3. If possible I'd like to avoid iptables. Also, since there are many instance of ffmpeg running, find out traffice for each and every instance is vital. I don't know if iptables can trace process or not??

已采纳解答

原帖链接 · xrfang · CC BY-SA 4.0

I found that "nethogs" has some command line options, especially -t (trace mode) which is suitable for use in scripts:

nethogs -s -t -c 5 eno1 2>/dev/null | grep ffmpeg

This will count all traffic on the eno1 interface for 5 seconds, then quits.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。