How can i print the output of vnstat -l
to a file?
what i've tried:
vnstat -l &> file
but it just hangsup
Remove the l
option. From the vnstat
man page:
-l, --live mode
Display current transfer rate for the selected interface in real time until interrupted. Statis‐
tics will be shown after interruption if the runtime was more than 10 seconds. An optional mode
parameter can be used to select between the displaying of packets per second (mode 0) and trans‐
fer counters (mode 1) during execution. --style can also be used to affect the layout of the
output.
So, the -l
makes vnstat
display output in "live mode", constantly updating, that's why you can't capture the output. If you want an easily parseable format use
vnstat --dumpdb
To just save the standard output do
vnstat > log.txt
If what you want is the current upload and download rate, vnstat
is not the right tool for the job. Try sar
from the sysstat
package (see man sar
for more info):
sudo apt-get install sysstat
sudo sar -n DEV 1 1
To extract the current* download and upload rate for the interface wlan0
from sar
's output do:
sudo sar -n DEV 1 1 | grep wlan0 | tail -n 1 | gawk '{print "Down: "$5,"Up: "$6}'
* What does "current" mean? sar
(and any other method) will take some milliseconds to generate output.
I don't know if you want to print just final summary to file from some period you are going to monitor network activity, but you can cheat it this way:
vnstat -l -i <device> >> dump.txt
The only issue is that vnstat will write output to file every second so it is not the most convenient way. For continuous monitoring with log for every second or so terdon answered your question.
vnstat
overwrites the status line again and again using backspaces so in the output you will se only the last status. You can replace the sequences of backspaces and expand the statuses to individual lines using for example: sed -re $'s/\b[\b ]+\b/\\n/g' dump.txt
Nov 20, 2014 at 14:53