用nethogs实时监控每个程序的流量使用

适用于(没有cron和init的)debian 8.0

  • 安装nethogs
  • 创建/root/monitor.sh

    TIMEOUT=3600
    TIMESTAMP=`date +%b_%d_%Y_%H_%M_%S`
    OUTPUT=/var/log/nethogs/$TIMESTAMP.log
    bash -ic "(/usr/local/sbin/nethogs -t -d 30&>$OUTPUT;kill 0;) | (sleep $TIMEOUT;kill 0; )" 3>&1 2>/dev/null
  • /etc/systemd/system/nethogs.timer

    [Unit]
    Description=Nethogs Daemon
    [Timer]
    OnBootSec=1min
    OnUnitActiveSec=1h
    [Install]
    WantedBy=timers.target
  • /etc/systemd/system/nethogs.service

    [Unit]
    Description=Nethogs run
    [Service]
    Type=oneshot
    ExecStart=/root/monitor.sh
  • 查看使用情况用Nethogs parser

    ./hogs -type=pretty /var/log/nethogs/Jan_03_2017_09_20_36.log