# Thresholds - these may depend on what you do with your computer and what # you want it to stay awake for. CPUTHRESH=5 # in percent DISKTHRESH=500 # in blocks/sec (the sar manpage says blocks are 512 bytes in normal cases) NETTHRESH=10 # in kB/s # (no sound playing is ~25 while playing mp3 w/ pcm+master turned all the way to zero is ~250) # Sample times (in seconds) - again needs may vary CPUTIME=1 DISKTIME=2 NETTIME=2 # Check CPU usage defined as non-idle time. echo "Check CPU" CPU=`sar $CPUTIME 1 | awk '/Durchschn/ { printf "%d\n",100-$8}'` echo "CPU = $CPU%" if [ $CPUTHRESH -lt $CPU ];then echo CPU is working, not sleeping exit; fi # Check disk usage (read + write) echo "CheckDisk" DISK=`sar -b $DISKTIME 1 | awk '/Durchschn.:/ { printf "%d\n",$5 + $6 }'` echo "DISK = $DISK" if [ $DISKTHRESH -lt $DISK ];then echo Disk is working, not sleeping exit; fi # Check net usage (exclude wifi0 which appears to have activity all the time for some weird reason) NET=`sar -n DEV $NETTIME 1 | awk '/Durchschn.:/ { SUM += $5 + $6 } END { printf ("%d\n",SUM)}'` echo "NETWORK = $NET" if [ $NETTHRESH -lt $NET ]; then echo Network is in use, not sleeping exit; fi #check for samba logins SAMBAUSERS=`smbstatus -p | awk /^[0123456789]+/'{printf("%d\n",$1)}'` if [ -n "$SAMBAUSERS" ]; then echo Some SAMBA users connected exit; fi # check for remote login REMOTELOGIN=`last | head | grep "pts/.*still logged in"` if [ -n "$REMOTELOGIN" ];then echo Remote login active: echo $REMOTELOGIN echo Not sleeping exit; fi if [ -e /tmp/suspend.last ];then if [ $(($(date +%s) - $(date -r /tmp/suspend.last +%s))) -lt 1800 ]; then echo between two suspends need to be a half hour exit fi fi if [ -e /tmp/suspend.lock ]; then echo some other suspend is running exit fi # Go into standby touch /tmp/suspend.lock echo "go into standby" echo 3 > /proc/acpi/sleep rm /tmp/suspend.lock touch /tmp/suspend.last