Another script I regularly use. It sets an alarm that slowly raises the volume of mpd to wake me up nicely to my own music. For power saving and noise considerations it can put the PC in sleep mode to be woken up just before the alarm goes off.

Usage

You first have to make a playlist for mpd that contains the music you want to be played. I use ncmpcpp for this. After that you just call the script with three arguments. The first argument is the time you want the alarm to go off in the format %H%M. This means 07:15 is 0715 and 23:30 is 2330. It will fire at the first occurrence of that time. The second argument contains the maximum volume for the alarm to reach in percents. Don't set this too low, or you won't wake up! The last argument is a boolean value that decides if the computer should go to sleep after setting the alarm. This is useful when you don't want to sleep next to a roaring fan. NOTE: Mpd has to be running for this alarm to work. I use an edited version that falls back on beeping if I somehow don't have mpd running, just leave a comment if you want me to put it up.

Dependencies

Check your favourite package manager for these.

If you get any "command not found" errors, install the mentioned command.

The script

The script itself:

#!/bin/sh

LOGFILE="/var/log/alarmlog"
TIMEFORALARM=$1
ALARMVOLUME=$2
GOTOSLEEP=$3
exec >> $LOGFILE 2>&1

# Allows the user to cancel any alarms by issuing cancel as an argument
if [ "$TIMEFORALARM" = "cancel" ]; then
    echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: killed all alarms"
    killall alarmclock
    exit
fi

# If user wants system to sleep, do so
if [ "$GOTOSLEEP" -ne "0" ]
then
    TIMEINSECSNOW="$(date +%s)"
    TIMEINSECSALARM="$(date -d "$TIMEFORALARM" "+%s")"
    if [ $TIMEINSECSALARM -gt $TIMEINSECSNOW ]
    then
        echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: set for $(date -d @$TIMEINSECSALARM +%c). Will go to sleep now."
        rtcwake -l -m mem -t $TIMEINSECSALARM > /dev/null
        echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: pc has woken up"
    else
        # If the time given is less than the time it is now, the alarm is probably for tomorrow, add 24 hours
        echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: set for $(date -d @$TIMEINSECSALARM +%c). Will go to sleep now."
        TIMEINSECSALARM=$(( $TIMEINSECSALARM + 86400 )) # 86400 is the seconds in 24 hours
        rtcwake -l -m mem -t $TIMEINSECSALARM > /dev/null
        echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: pc has woken up"
    fi
else
    echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: set for $(date -d @$TIMEINSECSALARM +%c)."
fi

# Wait until it's time to fire
until [ "$(date +'%H%M')" -eq $TIMEFORALARM ]; do
    sleep 2
done
echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: fired alarm for $TIMEFORALARM"

mpc -q clear
mpc -q load wakeup > /dev/null # We need to send this to /dev/null as not to clutter our log files

# Set the speaker to 100% to make sure the alarm will be heared
amixer -q sset 'Speaker' 100%
amixer -q -c 0 sset 'Speaker',0 unmute
amixer -q sset 'PCM' 100%

# Gracefully increase the volume of our music
amixer -q -c 0 sset 'Master',0 unmute
mpc -q play
for (( x=0; x<$ALARMVOLUME; x++ )); do
    amixer -q sset Master $x%
    sleep 0.4
done

Fun hacks

Awesome window manager

I have it bound to Meta+F12 with the following lines in AwesomeWM to make it easy for me to set it when I'm tired.

awful.key({ altkey }, "F12", function ()
awful.prompt.run({ prompt = "Alarm clock (time volume sleep): " }, promptbo    x[mouse.screen].widget,
    function (time)
        exec("sudo /home/myusername/Scripts/alarmclock "..time)
    end)
end)

Keep in mind that this script needs root access to work. I use sudo for this, because it allows me to set the following in the sudoers file using 'visudo':

myusername ALL=(ALL) NOPASSWD:/home/myusername/Scripts/alarmclock

Annoying beeps

If you're one of the people that can sleep through an air raid, consider adding the following lines of code. It adds a rather annoying series of beeps.

#Make sure you have the "beep" command installed
#This beeps 25 times, edit the -r value to your liking
amixer -q -c 0 sset 'Beep',0 unmute
amixer -q sset Beep 100%
sudo beep -f2500 -l500 -d300 -r25
amixer -q sset Beep 0%

Disclaimer

I shouldn't have to say this, but test the thing thoroughly before using it. We wouldn't want you to be late for work now, would we?


Comments