SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-05-13 03:06:05

rkwurth
#! Junkie
Registered: 2012-09-04
Posts: 365

Bash script to sleep on keyboard/mouse inactivity

Here is a script to sleep on keyboard and mouse inactivity. This is useful if you want power management that works the same in and outside of X. May conflict with other power management solutions that provide the same function.

#!/bin/bash

# Seconds of inactivity until sleep. 0 to disable sleep
bat_time_out=300
ac_time_out=0
# Programs worth staying up for :)
excluded_progs="shutdown mpg123"

# The target may depend on your system. i8042 is mouse and keyboard on mine (Thinkpad x230)
log=/proc/interrupts
target=i8042

check_ac()
{ # Adjust or disable timeout if plugged in
	on_ac_power
	if [ $? == 0 ]; then
		time_out=$ac_time_out
	else
		time_out=$bat_time_out
	fi
}

insomnia() 
{ # If any special processes are running, echo true
	for i in $excluded_progs ; do 
		if [ `pgrep $i` ]; then 
			echo true 
			return 
		fi 
	done 
}  

check_insomnia()
{ # If no special processes are running, echo false
	insomnia && [ -z `insomnia` ] && echo false
}

measure_activity() 
{ # Do all the stuff...
	count=$1
	check_ac
	# Do the actual checking for key/mouse interrupts
	interrupts_start=`grep $target $log | awk '{ print $2 }'`
	interrupts_stop=`sleep 1 && grep $target $log | awk '{ print $2 }'`

	if [ "$interrupts_start" == "$interrupts_stop" ] ; then
		((count++))
		if [ $count -eq $time_out ] && [ `check_insomnia` == "false" ] ; then
			/usr/sbin/pm-suspend
			measure_activity 0
		else
			measure_activity $count
		fi
	else
		measure_activity
	fi
}

measure_activity 0 & # I've tacked an & at the end for daemon-like behavior

Offline

Be excellent to each other!

#2 2014-02-14 20:07:20

goarilla
New Member
Registered: 2014-02-14
Posts: 1

Re: Bash script to sleep on keyboard/mouse inactivity

Why is measure_activity recursive ?

Offline

#3 2014-02-14 23:11:20

porkpiehat
#! Die Hard
Registered: 2012-10-02
Posts: 805

Re: Bash script to sleep on keyboard/mouse inactivity

The OP hasn't posted since September last year. It seems unlikely that he will respond to your question.

http://crunchbang.org/forums/profile.php?id=18632

Last edited by porkpiehat (2014-02-14 23:11:49)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo