[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: looking for a clock, minimum and can play sound



T o n g wrote:
> The following shell script will work:
> 
> --------------------------
> #!/bin/sh
> sleep $1
> beep
> -------------------------
> 
> name it 'alarm' and 'alarm 30' will alarm you after 30 seconds. 

That works if you know how many seconds to wait.  Let's say that it is
Mon, 17 Jan 2011 12:08:08 -0700 and you want an alarm to trigger at
3pm local time at Mon, 17 Jan 2011 15:00:00 -0700.  Then you know that
you can sleep for 10312 seconds.

  alarm-sleep 10312

That is great.  But calculating that value when you are sleepy isn't
for everyone.  Instead I would program that into the script to do for
you.  Use GNU date's --date extension to calculate the date from a
given date string.

  until="3pm"
  now=$(date +%s)
  then=$(date -d "$until" +%s)
  delay=$(( $then - $now ))
  sleep $delay

Then you can say something like:

  sleep-until-do   3pm   some command with args here

Below is a script I call sleep-until-do which sleeps until the given
time and then invokes the given command.  I use it to turn on the
radio to listen to the news at the top of the hour.  Perhaps you will
find it useful too.

Bob

#!/bin/sh

# Copyright 2009, 2010, 2011 Bob Proulx <bob@proulx.com>
# You may freely use, modify and/or distribute this file.

if [ $# -lt 1 ]; then
  echo "Error: Missing time argument" 1>&2
  exit 1
fi

if [ $# -lt 2 ]; then
  echo "Error: Missing do-command argument" 1>&2
  exit 1
fi

until=$1
shift

echo "Info: $until interpreted as:" $(date -R -d "$until")

now=$(date +%s)
then=$(date -d "$until" +%s)
delay=$(( $then - $now ))

if [ $delay -gt 2 ]; then
  echo Sleeping $delay seconds...
  sleep $delay
else
  echo Info: Time has passed.
fi

echo Invoking: "$@"
exec "$@"

Attachment: signature.asc
Description: Digital signature


Reply to: