I have a media PC (that is also a web test server on the LAN) that I want to reboot every so often as long as it is not in use. I have tests in place for the server using kodi, myth etc (its the display for kodi as well), however, I need to test if anything is using it over the network (file copying, developing websites, watching something etc).

I figure the easiest way to do this is find out how much data has been transferred over the network in the last 3mins or so. How can I do this from a bash script (.sh file run by cron)?

1 Answer 1


OK, solution found by using rx_bytes and tx_bytes (received and transmitted bytes, can also use packets or check for dropped packets, see: http://xmodulo.com/measure-packets-per-second-throughput-high-speed-network-interface.html)

The script:


R1=$(cat /sys/class/net/eth0/statistics/rx_bytes)
T1=$(cat /sys/class/net/eth0/statistics/tx_bytes)
sleep $test_time
R2=$(cat /sys/class/net/eth0/statistics/rx_bytes)
T2=$(cat /sys/class/net/eth0/statistics/tx_bytes)

tot=$(( (R2 + T2 - R1 - T1) / 1024 ))

echo $tot

Example of use (remember to throw in the amount of time in seconds):

test_time=10 ~/test.sh

This would check how many KB were sent/received over a 10 second period. Of course you can edit the script to include only R or T if you just want the sent or received value etc. This command will work from another .sh file or within the terminal, over SSH etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.