Howto: auto-shutdown after period of inactivity

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
steel_j
Posts: 4
Joined: Sat 08 Nov 2008, 00:25
Location: Montreal, Canada

Howto: auto-shutdown after period of inactivity

#1 Post by steel_j »

Hi, new Puppy user, old Linux user :D

I'm am running Puppy 4 on my HP Pavilion ZE-5700 series laptop.

I quickly noticed there was no real support for auto-suspend to RAM or disk or any type of hibernate. None that worked for me anyway.

Big distros support this very well on my portable,but they are sluggish and I moved to Puppy for speed and usability.

I did some research and there is very little info available. I noticed also that I did not need to suspend because Puppy is by design so quick to boot that it is faster from a cold boot than any disro I know resuming from sleep states.

But....I still needed Puppy to poweroff by itself when I went away.

I found a script and modified it for Puppy. It uses Puppy's own native shutdown script to properly halt the sysem.

You can modify it with your own time and add up to 2 applications that will prevent shutdown if they are found to be running. My own setup is 30 minutes of inactivity (based on mouse / touchpad use) and If mplayer or gxine are running the shutdown will not occur (In case I am watching a movie).

Save this to your "startup" folder as mouseup.sh and give it permision (chmod +rx). It will startup at boot by itself. I tested it and it works flawless.

Code: Select all

#!/bin/bash
# /root/startup/mouseup (2008-11-24)
# A script for testing user activity on their mouse
# and shutting down if given programs (2) are not running
# and there has been no mouse activity for declared period
# off time. Run as root

TIME=30m #set time between checks, "m" suffix for minutes
LOGFILE=/var/log/mouseup.log #this is where all activity will be logged

test -n "$TIME"
if [ $? -eq 1 ]; then
echo -e "\nYou have to declare how many minutes delay"
echo -e "between 1st and 2nd check for mouse activity.\n"
echo -e "Usage: mouseup [minutes]\n"
exit
fi

echo "`date` Starting mousup. Delay time set to $TIME." > $LOGFILE
# By using single ">" a new log is created. All others are ">>" adding new lines to existing logfile.

while true
do
MOUSE1=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE1 equals $MOUSE1" >> $LOGFILE

sleep $TIME

ps -A | grep "mplayer" > /dev/null 2>&1 #Replace with your own software
if [ $? -eq 0 ] ; then
PROGA_CHK=0
echo "`date` Mplayer is running" >> $LOGFILE 
else
PROGA_CHK=1
echo "`date` Mplayer is not running." >> $LOGFILE
fi

ps -A | grep "gxine" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
PROGB_CHK=0
echo "`date` Gxine is running" >> $LOGFILE
else
PROGB_CHK=1
echo "`date` Gxine is not running." >> $LOGFILE
fi

MOUSE2=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE2 equals $MOUSE2" >> $LOGFILE


if [ $MOUSE1 -eq $MOUSE2 -a $PROGA_CHK -eq 1 -a $PROGB_CHK -eq 1 ] ; then
echo "`date` shutdown -h now" >> $LOGFILE
echo "shutting down"
#/usr/X11R7/bin/restartwm ---for testing purposes---
/usr/X11R7/bin/wmpoweroff
exit
fi
done

#End of script
This original version of this script can be found here:
http://www.linuxquestions.org/questions ... wn-525935/

User avatar
jrb
Posts: 1536
Joined: Tue 11 Dec 2007, 19:56
Location: Smithers, BC, Canada

#2 Post by jrb »

Thanks steel_j
Followed your instructions, changed programs, changed time. Worked perfect! :D

User avatar
steel_j
Posts: 4
Joined: Sat 08 Nov 2008, 00:25
Location: Montreal, Canada

Glad to help...

#3 Post by steel_j »

Glad to help

Also, if you don't want it to shutdown when you are dowloading a large file you can put your browser as one of the two program that prevent shutdowns (i.e Firefox or Seamonkey)

User avatar
vtpup
Posts: 1420
Joined: Thu 16 Oct 2008, 01:42
Location: Republic of Vermont
Contact:

#4 Post by vtpup »

Thanks, this will come in very handy.

hitchup
Posts: 36
Joined: Sat 27 Sep 2008, 13:32
Location: Arkansas USA

#5 Post by hitchup »

Great script. Simply works. This should included in puppy.
Toshiba A205:S4777; Vista HP(Fading); Puppy Live CD(Rising)

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#6 Post by Pizzasgood »

Cool, except for two points.

A: you should also check the keyboard, not just the mouse.
B: that script assumes the mouse interrupt is 12. Mine is not. Maybe this is because I use a usb mouse? I don't know whether those interrupt numbers are standardized. Anyways, my mouse (actually, the usb hub it's plugged into) is at interrupt 23. Also, the thread you linked to states that keyboards are on interrupt 2, but mine is at 1.

Code: Select all

# cat /proc/interrupts 
           CPU0       
  0:         66   IO-APIC-edge      timer
  1:      49768   IO-APIC-edge      i8042
  3:          2   IO-APIC-edge    
  4:          2   IO-APIC-edge    
  6:          3   IO-APIC-edge      floppy
  7:          0   IO-APIC-edge      parport0
  8:          1   IO-APIC-edge      rtc
  9:          1   IO-APIC-fasteoi   acpi
 14:      42920   IO-APIC-edge      ata_piix
 15:          0   IO-APIC-edge      ata_piix
 16:    1540246   IO-APIC-fasteoi   uhci_hcd:usb4, HDA Intel
 18:    9042926   IO-APIC-fasteoi   uhci_hcd:usb3, bttv0
 19:      36804   IO-APIC-fasteoi   ata_piix, uhci_hcd:usb2
 22:     951334   IO-APIC-fasteoi   eth0
 23:     430266   IO-APIC-fasteoi   uhci_hcd:usb1, ehci_hcd:usb5
NMI:          0   Non-maskable interrupts
LOC:   12166285   Local timer interrupts
TRM:          0   Thermal event interrupts
SPU:          0   Spurious interrupts
ERR:          0
MIS:          0
#
I think a key item here is the 'i8042' that is displayed on my interrupt 1. My understanding is that the 'i8042' corresponds to the PS/2 keyboard and mouse ports. So you could use grep to grab that line without having to hardcode an interrupt. But that doesn't take care of a USB keyboard or mouse. Interrupt 23 corresponds to my USB hub, not the mouse. Anything else plugged into the hub will also show activity. In some ways that's good - if I play with my gamepad or wacom tablet it will show activity. So you could grep to find all the USB entries too. But there might be times when that's wrong, such as if you have a USB drive, in which case the system's normal operation might show activity there. On my system, interrupts 16 and 18 are changing even when the machine is idle. I believe one of them corresponds to my printer. I don't remember what the other is, and I don't feel like crawling under my bed to find out. I'd rather crawl into bed, and will do that shortly.

I suppose you could have it try to detect whether the user was using a usb or a ps/2 mouse first before choosing what to monitor. Or you could just explain how to determine the corresponding interrupt and let the program accept another parameter so the user can specify without having to edit the script. (They can check by setting up a while loop that cat's the file and then sleeps for a second, then play around with wiggling the mouse and see which numbers only change when the mouse is active. Same for keyboard and other devices.)

Anyways, I'm starting to have trouble typing. G'night.
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

praveenmallar
Posts: 7
Joined: Wed 10 Dec 2008, 16:15

#7 Post by praveenmallar »

I'm using Asus eeepc with puppy 4.1

is there a way to shutdown puppy on lid-closure of the laptop?

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#8 Post by Pizzasgood »

I haven't looked into it yet, but I think in some laptops the lid-close signal registers as a keyboard stroke. You might try using xev to see if that's the case with yours, and which keycode it uses. Then you could map that keycode to do whatever you want.
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

ferikenagy
Posts: 53
Joined: Mon 20 Nov 2006, 08:26
Contact:

cups inactivity too

#9 Post by ferikenagy »

we are using puppy as an CUPS LAN printing server too.this computer can be power on remotely from LAN using Wake On Lan (WOL) command from a remote (windows/Linux computer). My problem was how to stop it, after not using it.First off all I tried with using tightVNC server on it, permitting to login from remote and to power down the computer, but it was not confortable, and it happens often that simply we forgot to power off remotely or locally the computer after printing. So I was very glad of this posted script, and I completed with keyboard and cups monitoring activity.The cups activity is monitored by comparing the size of "/var/cups/log/page_log" file.
the modified script is (mouseup):
##############

Code: Select all

#!/bin/bash
# /root/startup/mouseup (2008-11-24)
# A script for testing user activity on their mouse
# and shutting down if given programs (2) are not running
# and there has been no mouse activity for declared period
# off time. Run as root

TIME=15m #set time between checks, "m" suffix for minutes
LOGFILE=/var/log/mouseup.log #this is where all activity will be logged

test -n "$TIME"
if [ $? -eq 1 ]; then
echo -e "\nYou have to declare how many minutes delay"
echo -e "between 1st and 2nd check for mouse activity.\n"
echo -e "Usage: mouseup [minutes]\n"
exit
fi

echo "`date` Starting mousup. Delay time set to $TIME." > $LOGFILE
# By using single ">" a new log is created. All others are ">>" adding new lines to existing logfile.

while true
do
MOUSE1=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE1 equals $MOUSE1" >> $LOGFILE
KEYB1=`cat /proc/interrupts | grep ' 1:' | awk '{print $2}'`
echo "`date` KEYB1 equals $KEYB1" >> $LOGFILE
CUPS1=`ls -g /var/cups/log/page_log | cut -d ' ' -f 4`
echo "`date` CUPS1 equals $CUPS1" >> $LOGFILE 
sleep $TIME

ps -A | grep "mplayer" > /dev/null 2>&1 #Replace with your own software
if [ $? -eq 0 ] ; then
PROGA_CHK=0
echo "`date` Mplayer is running" >> $LOGFILE
else
PROGA_CHK=1
echo "`date` Mplayer is not running." >> $LOGFILE
fi

ps -A | grep "gxine" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
PROGB_CHK=0
echo "`date` Gxine is running" >> $LOGFILE
else
PROGB_CHK=1
echo "`date` Gxine is not running." >> $LOGFILE
fi

MOUSE2=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE2 equals $MOUSE2" >> $LOGFILE
KEYB2=`cat /proc/interrupts | grep ' 1:' | awk '{print $2}'`
echo "`date` KEYB2 equals $KEYB2" >> $LOGFILE
CUPS2=`ls -g /var/cups/log/page_log | cut -d ' ' -f 4`
echo "`date` CUPS2 equals $CUPS2" >> $LOGFILE 

if [ $MOUSE1 -eq $MOUSE2 -a $KEYB1 -eq $KEYB2 -a $CUPS1 -eq $CUPS2 -a $PROGA_CHK -eq 1 -a $PROGB_CHK -eq 1 ] ; then
echo "`date` shutdown -h now" >> $LOGFILE
echo "shutting down"
echo " poweroff "
sleep 2
exec /usr/X11R7/bin/wmpoweroff   #off in X grafic mode
sleep 2 
/sbin/poweroff	#power off in text mode,if was not previous in X
exit
fi
echo "reluare ciclu $TIME"
done

#End of script

################


and it started by script (startmouseup):
##############

Code: Select all

#!/bin/sh
case "$1" in
	start)
	echo " starting no activity mouse/keyboard/cups-> power off"
	/usr/local/mouseup/mouseup&
			;;
esac
#############

both resize on path /usr/local/mouseup

and I make a link /usr/local/mouseup/startmouseup to /etc/init.d/ to be automatically started at boot up.I preferred not to boot from X win by putting link in /root/Startup directory, because maybe in future I shall disable Xwin, Xserver automatically launch, if I use computer from remote as LAN printer

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#10 Post by amigo »

Can you please edit your post to enclose the code in CODE tags?

User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

#11 Post by Dougal »

I think I found a daft way to handle USB devices...
I'm not sure what will happen if you're using a hub and maybe my search in /sys could produce more than one result...
I also suspect there's a chance that if your mouse/keyboard is connected via a hub that also has some kind of drive (pendrive or external HD), there's a chance that there'll be some periodic interrupts as a result of that...

I'm including a script that checks both USB and PS/2. (I'm not sure if the indentation won't get messed up, so I'm including a gzipped copy of it, too.
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

thoase
Posts: 115
Joined: Fri 03 Apr 2009, 17:24
Location: Sweden

#12 Post by thoase »

But, where is my "startup" folder?

I tried to put mouseup.sh in my home-folder where intrd.gz, vmlinux and *.sfs-files are. But it wasn't the right folder!!

ricstef
Posts: 52
Joined: Wed 02 Aug 2006, 02:25
Location: Woodstock, ON. Canada

Startup folder

#13 Post by ricstef »

@thoase
But, where is my "startup" folder?
Try
/root/Startup

Hope that helps. :)
Richard

puttingau
Posts: 17
Joined: Fri 12 Jun 2009, 10:24

include network activity

#14 Post by puttingau »

I modified this to take into account network activity, when another user is accessing the internet through internet connection sharing. This script sits on the gateway machine, and won't shutdown if there is another user accessing the internet through it. Thanks to all on this post, and those on http://ubuntuforums.org/archive/index.php/t-530973.html
#!/bin/bash
# /root/startup/mouseup (2008-11-24)
# A script for testing user activity on their mouse
# and shutting down if given programs (2) are not running
# and there has been no mouse activity for declared period
# off time. Run as root

TIME=31m #set time between checks, "m" suffix for minutes
LOGFILE=/home/pete/mouseup.log #this is where all activity will be logged

test -n "$TIME"
if [ $? -eq 1 ]; then
echo -e "\nYou have to declare how many minutes delay"
echo -e "between 1st and 2nd check for mouse activity.\n"
echo -e "Usage: mouseup [minutes]\n"
exit
fi

echo "`date` Starting mouseup. Delay time set to $TIME." > $LOGFILE
# By using single ">" a new log is created. All others are ">>" adding new lines to existing logfile.

while true
do
MOUSE1=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE1 equals $MOUSE1" >> $LOGFILE
#find the value of RX received packets to compare later for network activity
RX=`/sbin/ifconfig eth1 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
echo "Initial RX: " $RX >> $LOGFILE
#add 1000 bytes;If a new RX measurement later, is less than this, some real network activity would have occurred
T_RX=`expr $RX + 300`
echo "Threshold RX: " $T_RX >> $LOGFILE

sleep $TIME

ps -A | grep "mplayer" > /dev/null 2>&1 #Replace with your own software
if [ $? -eq 0 ] ; then
PROGA_CHK=0
echo "`date` Mplayer is running" >> $LOGFILE
else
PROGA_CHK=1
echo "`date` Mplayer is not running." >> $LOGFILE
fi

ps -A | grep "VirtualBox" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
PROGB_CHK=0
echo "`date` VirtualBox is running" >> $LOGFILE
else
PROGB_CHK=1
echo "`date` VirtualBox is not running." >> $LOGFILE
fi

#now check if RX value has increased
NEW_RX=`/sbin/ifconfig eth1 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
echo "NEW_RX: " $NEW_RX >> $LOGFILE

if [ $NEW_RX -gt $T_RX ] ; then
NETWORK_CHK=0
echo " NEW_RX > threshold, a network user exists. " >> $LOGFILE
else
NETWORK_CHK=1
echo " NEW_RX < threshold, no network activity. " >> $LOGFILE
fi
MOUSE2=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE2 equals $MOUSE2" >> $LOGFILE
echo "MOUSE1 " $MOUSE1 " MOUSE2 "$MOUSE2 " PROGA_CHK " $PROGA_CHK " PROGB_CHK "$PROGB_CHK " NETWORK_CHK " $NETWORK_CHK >> $LOGFILE

if [ $MOUSE1 -eq $MOUSE2 -a $PROGA_CHK -eq 1 -a $PROGB_CHK -eq 1 -a $NETWORK_CHK -eq 1 ] ; then
echo "`date` shutdown -h now" >> $LOGFILE
echo "shutting down"
#/usr/X11R7/bin/restartwm ---for testing purposes---
#/usr/X11R7/bin/wmpoweroff
shutdown -h now
exit
fi
done

#End of script
Thank you steel_j , suits my needs perfectly

rollo5
Posts: 4
Joined: Thu 07 Mar 2013, 18:56

all mouse support + network up and download

#15 Post by rollo5 »

Hallo,

this is my first post. Puppy Linux is great!

I modified the script. Now all mouses are recognized because the script checks the mouse activity by the coordinates. It also checks the TX received packets, so the PC waits for up or download to finish.

It checks every minute for testing, you should change it.

Code: Select all

#!/bin/bash
# /root/startup/mouseup (2008-11-24)
# A script for testing user activity on their mouse
# and network activity
# and shutting down if given programs (2) are not running
# and there has been no mouse activity
# and network activity for declared period
# off time. Run as root

TIME=1m #set time between checks, "m" suffix for minutes
#LOGFILE=/mnt/home/mouseup.log #this is where all activity will be logged

test -n "$TIME"
if [ $? -eq 1 ]; then
echo -e "\nYou have to declare how many minutes delay"
echo -e "between 1st and 2nd check for mouse activity.\n"
echo -e "Usage: mouseup [minutes]\n"
exit
fi

#echo "`date` Starting mouseup. Delay time set to $TIME." > $LOGFILE
# By using single ">" a new log is created. All others are ">>" adding new lines to existing logfile.

while true
do
XP1=$(getcurpos | cut -d" " -f1)
YP1=$(getcurpos | cut -d" " -f2)
#echo "XP1=$XP1" >> $LOGFILE
#echo "YP1=$YP1" >> $LOGFILE
#echo "CURPOS1=$CURPOS1" >> $LOGFILE

#find the value of RX received packets to compare later for network activity
RX=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
#echo "Initial RX: " $RX >> $LOGFILE
#add 1000 bytes;If a new RX measurement later, is less than this, some real network activity would have occurred
T_RX=`expr $RX + 300`
#echo "Threshold RX: " $T_RX >> $LOGFILE

TX=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
T_TX=`expr $TX + 300`

sleep $TIME

ps -A | grep "mplayer" > /dev/null 2>&1 #Replace with your own software
if [ $? -eq 0 ] ; then
PROGA_CHK=0
#echo "`date` mplayer is running" >> $LOGFILE
else
PROGA_CHK=1
#echo "`date` mplayer is not running." >> $LOGFILE
fi

ps -A | grep "virtualbox" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
PROGB_CHK=0
#echo "`date` virtualbox is running" >> $LOGFILE
else
PROGB_CHK=1
#echo "`date` virtualbox is not running." >> $LOGFILE
fi

#now check if RX value has increased
NEW_RX=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
#echo "NEW_RX: " $NEW_RX >> $LOGFILE
NEW_TX=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`

if [ $NEW_RX -gt $T_RX ] ; then
NETWORK_CHK_RX=0
#echo " NEW_RX > threshold, a network user exists. " >> $LOGFILE
else
NETWORK_CHK_RX=1
#echo " NEW_RX < threshold, no network activity. " >> $LOGFILE
fi

if [ $NEW_TX -gt $T_TX ] ; then
NETWORK_CHK_TX=0
#echo " NEW_RX > threshold, a network user exists. " >> $LOGFILE
else
NETWORK_CHK_TX=1
#echo " NEW_RX < threshold, no network activity. " >> $LOGFILE
fi

XP2=$(getcurpos | cut -d" " -f1)
YP2=$(getcurpos | cut -d" " -f2)

if [ $XP1 -eq $XP2 -a $YP1 -eq $YP2 -a $PROGA_CHK -eq 1 -a $PROGB_CHK -eq 1 -a $NETWORK_CHK_RX -eq 1 -a $NETWORK_CHK_TX -eq 1 ] ; then
#echo "`date` shutdown -h now" >> $LOGFILE
#echo "shutting down"
#/usr/X11R7/bin/restartwm ---for testing purposes---
#/usr/X11R7/bin/wmpoweroff
wmpoweroff
exit
fi
done

#End of script 
I need this script for my self made server (printing and data transfer).

cheers
roland

Post Reply