15

I want to check the network bandwidth used by my process. For this i found that nethogs tool is useful. Using this tool i can see which process is eating up a network bandwidth and process behaviour. But how do I capture data from nethogs for a my process and store it into log file ?

vab050
  • 305
  • 1
  • 2
  • 10
  • possible duplicate of [redirecting console output to a file in unix](http://stackoverflow.com/questions/20702652/redirecting-console-output-to-a-file-in-unix) – l0b0 Dec 20 '13 at 12:09
  • @anubhava :Thanks for your time, but It is not working. it fails with error "Waiting for first packet to arrive (see sourceforge.net bug 1019381)" – vab050 Dec 20 '13 at 12:26
  • 2
    @l0b0: gone through the the link. nethogs won't gives the large amount of output. it just simply refresh the screen like top. is there another way to do it ? – vab050 Dec 20 '13 at 12:32

4 Answers4

4

You can run nethogs in background in tracemode and write output to a file like this:

sudo nethogs -t eth1 &> /var/tmp/nethogs.log & 

Download and build the nethogs-parser as described here.

Then after you have accumulated enough data you can run the parser to see the results:

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

Make sure to kill the running nethogs process when you are done collecting data.

More info here on automating the task.

kaptan
  • 3,060
  • 5
  • 34
  • 46
  • About automating the task at that site I first got instant empty logs. In monitor.sh I had to change `sh` by `/bin/bash` to get a report (first tried with 30 secs timeout) – dstonek Mar 25 '20 at 19:21
3

I dont know when these options got implemented but you can use nethogs -t or nethogs -b, the pid and user are strangely placed at the end of the pid command string, but easy enough to parse.

I think you need to use the latest cvs version 0.8.1-SNAPSHOT

Aquarius Power
  • 3,729
  • 5
  • 32
  • 67
  • 2
    For example, collecting data for 5 seconds and printing output once: `nethogs -c 1 -d 5 -t` – Richlv Oct 09 '16 at 08:13
  • 1
    @Richlv this is good, but for actual 5 second collection, `-c 2` seems to be necessary. Probably the first one doesnt count – phil294 Aug 26 '19 at 15:31
1

You can use this command to capture output:

nethogs -d 5 | sed 's/[^[:print:][:cntrl:]]//g' > output.txt
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

The right command of nethogs is

 nethogs -d 1 eth0 > output.txt

You need to specify the network interface otherwise, the default interface eth0 will be used. Sometime, nethogs might not show the proper output because of the network interface. It is always better to provide the network interface and generate some traffic during the experimentation. You can print the output to a file by adding > output.txt -d argument specifies how frequently the output will be shown. Here, I gave 1, this indicates that the output will be shown per second.

Hope this might be useful.

Arif A.
  • 933
  • 8
  • 14
  • That doesn't work either: `No devices to monitor. Use '-a' to allow monitoring loopback interfaces or devices that are not up/running` – GlorianChris May 19 '17 at 03:58
  • 1
    This doesn't work. Without `> output.txt` it shows me the output, but when I add redirect to file then only thing that is written to the file is `Ethernet link detected`. – Learner Dec 22 '20 at 10:16