WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: shutdown on idle network activity  (Read 197 times)

Offline aris

  • WikiUser
  • *
  • Posts: 30
shutdown on idle network activity
« on: November 09, 2017, 06:36:39 AM »
Is there any tcz for autoshutdown tcl if there is no network activity, kind of laptop mode but monitoring network activity?
I intent to (mostly) using TCL for NAS..and I need power management thing....shutdown the TCL if there is no traffic

I tried to duplicate other people script to do it in cron but its not working...
Code: [Select]
#!/bin/bash
#
# This is scheduled in CRON. It will run every 20 minutes
# and check for inactivity. It compares the RX and TX packets
# from 20 minutes ago to detect if they significantly increased.
# If they haven't, it will force the system to sleep.
#
log=~/scripts/idle/log
# Extract the RX/TX
rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
#Write Date to log
date >> $log
echo "Current Values" >> $log
echo "rx: "$rx >> $log
echo "tx: "$tx >> $log
# Check if RX/TX Files Exist
if [ -f ~/scripts/idle/rx ] || [ -f ~scripts/idle/tx ]; then
p_rx=`cat ~/scripts/idle/rx` ## store previous rx value in p_rx
p_tx=`cat ~/scripts/idle/tx` ## store previous tx value in p_tx
echo "Previous Values" >> $log
echo "p_rx: "$p_rx >> $log
echo "t_rx: "$p_tx >> $log
echo $rx > ~/scripts/idle/rx ## Write packets to RX file
echo $tx > ~/scripts/idle/tx ## Write packets to TX file
# Calculate threshold limit
t_rx=`expr $p_rx + 1000`
t_tx=`expr $p_tx + 1000`
echo "Threshold Values" >> $log
echo "t_rx: "$t_rx >> $log
echo "t_tx: "$t_tx >> $log
echo " " >> $log
if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then ## If network packets have not changed that much
echo "Shutting down" >> $log
echo " " >> $log
rm ~/scripts/idle/rx
rm ~/scripts/idle/tx
sudo poweroff
fi
#Check if RX/TX Files Doesn't Exist
else
echo $rx > ~/scripts/idle/rx ## Write packets to file
echo $tx > ~/scripts/idle/tx
echo " " >> $log
fi

cron it at /20" but machine still awake..
Any input appriciated...detailed step is expected....:D
« Last Edit: November 09, 2017, 06:38:12 AM by aris »

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1587
Re: shutdown on idle network activity
« Reply #1 on: November 09, 2017, 07:02:49 AM »
The script is using bash which is not necessary and can be changed to sh so bash.tcz doesn't need to be installed.
Also the script doesn't create directories where it attempts to write to. using /tmp might make more sense.
Code: [Select]
#!/bin/sh
#
# This is scheduled in CRON. It will run every 20 minutes
# and check for inactivity. It compares the RX and TX packets
# from 20 minutes ago to detect if they significantly increased.
# If they haven't, it will force the system to sleep.
#
log=/tmp/idle-log
# Extract the RX/TX
rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
#Write Date to log
date >> $log
echo "Current Values" >> $log
echo "rx: "$rx >> $log
echo "tx: "$tx >> $log
# Check if RX/TX Files Exist
if [ -f /tmp/idle-rx ] || [ -f /tmp/idle-tx ]; then
p_rx=`cat /tmp/idle-rx` ## store previous rx value in p_rx
p_tx=`cat /tmp/idle-tx` ## store previous tx value in p_tx
echo "Previous Values" >> $log
echo "p_rx: "$p_rx >> $log
echo "t_rx: "$p_tx >> $log
echo $rx > /tmp/idle-rx ## Write packets to RX file
echo $tx > /tmp/idle-tx ## Write packets to TX file
# Calculate threshold limit
t_rx=`expr $p_rx + 1000`
t_tx=`expr $p_tx + 1000`
echo "Threshold Values" >> $log
echo "t_rx: "$t_rx >> $log
echo "t_tx: "$t_tx >> $log
echo " " >> $log
if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then ## If network packets have not changed that much
echo "Shutting down" >> $log
echo " " >> $log
rm /tmp/idle-rx
rm /tmp/idle-tx
sudo poweroff
fi
#Check if RX/TX Files Doesn't Exist
else
echo $rx > /tmp/idle-rx ## Write packets to file
echo $tx > /tmp/idle-tx
echo " " >> $log
fi

Download a copy and keep it handy: Core book ;)

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 9612
Re: shutdown on idle network activity
« Reply #2 on: November 09, 2017, 10:00:27 AM »
Also, doublecheck if busybox cron supports that /20 syntax, if I read your post correctly.
The only barriers that can stop you are the ones you create yourself.

Offline aris

  • WikiUser
  • *
  • Posts: 30
Re: shutdown on idle network activity
« Reply #3 on: November 09, 2017, 08:40:20 PM »
Also, doublecheck if busybox cron supports that /20 syntax, if I read your post correctly.
I will start with this.
I tried /8 * * * sudo reboot...not working...not working on my tcl , but its working on my openwrt router..(I think same busybox)
Tried 59 10 * * sudo reboot ...working

I create directory as scripts requested /home/tc/scripts/idle/ run the scripts and its working but with no /20..using exact time
35 11 * * * sh /home/tc/idle.sh ..still the same scripts just change the name...
so no /20 for cron tinycore ?


Offline Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 5470
Re: shutdown on idle network activity
« Reply #4 on: November 09, 2017, 09:38:45 PM »
Hi aris
You could do it without cron by wrapping the script in a loop:
Code: [Select]
#!/bin/sh
#
# This is scheduled in CRON. It will run every 20 minutes
# and check for inactivity. It compares the RX and TX packets
# from 20 minutes ago to detect if they significantly increased.
# If they haven't, it will force the system to sleep.
#
log=/tmp/idle-log

while true
do
sleep 1200

# Extract the RX/TX
rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
#Write Date to log
date >> $log
echo "Current Values" >> $log
echo "rx: "$rx >> $log
echo "tx: "$tx >> $log
# Check if RX/TX Files Exist
if [ -f /tmp/idle-rx ] || [ -f /tmp/idle-tx ]; then
p_rx=`cat /tmp/idle-rx` ## store previous rx value in p_rx
p_tx=`cat /tmp/idle-tx` ## store previous tx value in p_tx
echo "Previous Values" >> $log
echo "p_rx: "$p_rx >> $log
echo "t_rx: "$p_tx >> $log
echo $rx > /tmp/idle-rx ## Write packets to RX file
echo $tx > /tmp/idle-tx ## Write packets to TX file
# Calculate threshold limit
t_rx=`expr $p_rx + 1000`
t_tx=`expr $p_tx + 1000`
echo "Threshold Values" >> $log
echo "t_rx: "$t_rx >> $log
echo "t_tx: "$t_tx >> $log
echo " " >> $log
if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then ## If network packets have not changed that much
echo "Shutting down" >> $log
echo " " >> $log
rm /tmp/idle-rx
rm /tmp/idle-tx
sudo poweroff
fi
#Check if RX/TX Files Doesn't Exist
else
echo $rx > /tmp/idle-rx ## Write packets to file
echo $tx > /tmp/idle-tx
echo " " >> $log
fi

done

 

Then launch the script from  bootlocal  in the background like this:
Code: [Select]
/home/tc/idle.sh &

Offline aris

  • WikiUser
  • *
  • Posts: 30
Re: shutdown on idle network activity
« Reply #5 on: November 11, 2017, 07:09:42 AM »
@rich..thank you...brillian suggestion...I will try it now
@misalf.. sh  it is...thanks

will report back...
On second thought...
I think compare traffic is not really a perfect solution for me
I think It will be better to simply the script to ..something like...if traffic for the last 10 minutes is less than 1 megs...than sudo poweroff
What do you think ...which is better for NAS...

Offline Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 5470
Re: shutdown on idle network activity
« Reply #6 on: November 11, 2017, 10:13:57 AM »
Hi aris
On second thought...
I think compare traffic is not really a perfect solution for me
I think It will be better to simply the script to ..something like...if traffic for the last 10 minutes is less than 1 megs...than sudo poweroff
What do you think ...which is better for NAS...
You would probably need to set a threshold to filter out any background traffic, such as NTP activity to keep th NASs clock set to
the correct time for example.

If your NAS maintains operational statistics (bytes sent/received, files sent/received, transfer speeds, etc.) that would be the
best data to use to determine whether or not to shut down.

Offline aris

  • WikiUser
  • *
  • Posts: 30
Re: shutdown on idle network activity
« Reply #7 on: November 15, 2017, 02:00:36 AM »
@Rich ... ;D I will keep the previous script...

report, for now
The scritps works just fine in bootlocal files...
Thanks to all...will report back in 1-2 weeks