I've been trying for some time now and unfortunately I can't get any further, so I'm hoping you can help me.
I would need to determine the total UP/DOWN traffic since start of the PC for a specific process. I have found nethogs which gives me the correct values (in the terminal) with the following command.
./nethogs -t -v 2 eth0 2>&1 | awk '/AB/{print $3,"/",$2}'
Output:
211 / 561
211 / 561
271 / 620
271 / 620
...
Now I would need the last (and therefore most recent) value to be saved in the first line in a text file so that I can process it further.
To save all values i have added >|/dev/shm/traffic.log
at the end. But the file is not updated but a new line is added every x seconds.
Unfortunately, I am failing and have not yet found a solution. I would like to ask you to help me here.
CodePudding user response:
@Author,
I created sample ./nethogs file to simulate your output at my local host.
$ ./nethogs -t -v 2 eth0
AB 211 561
AB 211 561
AB 271 620
AB 271 620
$ ./nethogs -t -v 2 eth0 2>1 | awk '/AB/{print $3,"/",$2}'
561 / 211
561 / 211
620 / 271
620 / 271
Hence I tried using valid redirection, without using OR operator.
$ ./nethogs -t -v 2 eth0 2>1 | awk '/AB/{ print $3,"/",$2}' >/dev/shm/traffic.log
$ cat /dev/shm/traffic.log
561 / 211
561 / 211
620 / 271
620 / 271
Hence replace:
>|/dev/shm/traffic.log
With:
>/dev/shm/traffic.log
If you are interested to get last output alone, you can use:
$ ./nethogs -t -v 2 eth0 2>&1 | tail -2 | awk '/AB/{print $3,"/",$2}' >/dev/shm/traffic.log
cat /dev/shm/traffic.log
620 / 271
620 / 271
CodePudding user response:
Thank you for the feedback, Unfortunately nothing is saved in the log when the "tail -2" command is before the "awk".
$ ./nethogs -t -v 2 eth0 2>&1 | tail -2 | awk '/AB/{print $3,"/",$2}' >/dev/shm/traffic.log
nethogs outputs permanent lines, with awk I filter the needed ones. The last output is actual sum of the TX and RX bytes. Only this I would like to have in the first line in the logfile.
so the first line in the logfile should always correspond to the last output of nethogs with awk filter. the old data should always be overwritten in the File.