Tiny Core Linux
General TC => General TC Talk => Topic started by: aris on November 09, 2017, 09: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...#!/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
-
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.
#!/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
-
Also, doublecheck if busybox cron supports that /20 syntax, if I read your post correctly.
-
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 ?
-
Hi aris
You could do it without cron by wrapping the script in a loop:
#!/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:
/home/tc/idle.sh &
-
@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...
-
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 the 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.
-
@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
-
I think it still went well..
the script did turn my tcl box on "iddle" traffic.
I think poweroff and wol is better than sleep in my environment
(using wd green not red, and atom board )
Thank You all