Sleep timer (suspend) script for Linux

I usually go to sleep listening to gnaural or watching old TV shows on the computer beside my bed (which my wife just loves). Rather than have it running all night, I’d rather have my computer sleep after a certain time.

For some reason, the last time I tried to write a script to suspend my computer (a few years ago), it was a serious pain in the ass. Now, thanks to pm-utils, it’s pretty easy. The command pm-suspend must be run as root, but aside from that it pretty much does what it says on the tin.

To use the script, copy and paste it into a text file (preferably called sleeptimer). Then, change the permissions on the file to make it executable. From the command line, you can do this with chmod u+x sleeptimer. From the command line, ./sleeptimer N – will sleep the computer in N minutes, where N is some positive integer. The script will go through a count down for each fifteen minutes and gives a warning at 10, 5 and then 1 minute before it sleeps.

Please forgive the crudity. I didn’t really write it for general public usage, but I’m posting it in response to a request.

#!/bin/bash
#
# sleeptimer.sh
#
# Author: Timothy A.V. Teatro <tim@timteatro.net>
# Date  : Jan 11, 2011
#
# Description: A rather unsophisticated little script that I wrote for
#    myself. Since I usually fall asleep listening to gnaural or watching
#    a movie on my computer beside my bed, I wanted something to suspend
#    my computer rather than using shutdown.
#
# Usage : sudo ./sleeptimer <integer>
#    where <integer> is the number of minutes before the computer is suspended
#
#    This script is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#####################################################

#
# Must be run as root, so check id at the door.
#
if [ "$(id -u)" != "0" ]; then
	echo Must be run as root.
	exit 1
fi

min=$1 # Keep the first argument on the command line here
elapsed=0 

#
# Check to be sure argument is a number...
#
if [ $min -eq $min 2> /dev/null ]; then
	#
	# ... and if it is, then decide what to do with it
	#
	# If it's less than 15 minutes, we want to count down to 10, 5 and
	# then 1 minute for feedback to the user.
	#
	if [ $min -lt 15 ]; then
		echo Sleeping in $min minutes [`date`]
		while [ $elapsed -le $min ]; do
			sleep 1m
			elapsed=$((elapsed+1))
			if [ $((min-elapsed)) -eq 10 ]; then
				echo Sleeping in 10 min.
			elif [ $((min-elapsed)) -eq 5 ]; then
				echo Sleeping in 5 min.
			elif [ $((min-elapsed)) -eq 1 ]; then
				echo Sleeping in less than 1 min.
			fi
		done
	else # If it's more, we'll count down in 15 minute intervals.
		#
		# Get the remainder of $min/15 and sleep that down.
		#
		remainder=`expr $min % 15`
		sleep ${remainder}m
		elapsed=$((elapsed+remainder))
		#
		# Now that the time remaining is a factor of 15, we'll sleep
		# in 15 minute incriments until the last 15 minutes of the
		# timer.
		#
		while [ $elapsed -lt $((min-15)) ]; do
			sleep 15m
			elapsed=$((elapsed+15))
			echo Sleeping in $((min-elapsed)) minutes. [`date`]
		done
		sleep 5m
		echo Sleeping in 10 minutes
		sleep 5m
		echo Sleeping in 5 minutes
		sleep 4m
		echo Sleeping in less than a minute.
		sleep 1m
	fi
	#
	# We've done our counting, now suspend.
	#
	echo "!!! NOW SUSPENDING YOUR MACHINE !!!"
	pm-suspend
	exit 0
else
	echo $min is not an integer. Please specify the number of minutes before sleep.
	exit 2
fi

No related posts.

3 Comments »

  1. Name * says:

    Do you have earphones on when you fall asleep? I couldn’t sleep with earphones. Thanks for posting.

    • Tim Teatro says:

      For the binural beat stuff, you sort of have to, but most often I just use a single earbud. I have my jack rigged for mono and I ripped one earbud off of a set of iPod buds (crappy things).

      Thanks for commenting :)

  2. ben says:

    Thx a lot, it works great.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.