« October 2008 | Main | January 2009 »

December 18, 2008

A Shell Script to Randomize Your Computer's Wake Time

Lately, we’ve been exploring the option of having our computers sleep after some idle time. One of our concerns was our nightly maintenance window; we wanted our computers to wake up in the middle of the night, but we didn't want to have them all wake up at exactly the same time. So, I wrote this script to randomize the time at which they wake up nightly:


#!/bin/sh

# a script to randomize the computer's wake time

pmset=`whereis pmset`
type=wakeorpoweron
days=MTWRFSU

# The maximum number of seconds past 3:15 AM to wake up.
maxsleep=5400

# Randomize the number of seconds.
sleeptime=`perl -e 'srand (time ^ $$ ^ unpack "%L*", \` ps axww | gzip\`);\
        print int(rand( $ARGV[0] ));' $maxsleep`

# Default time to wake up is 3:15 AM.
hours=3
minutes=15
seconds=0

# Convert the number of seconds into hour, minutes, seconds

# Subtract hours.
while [ $sleeptime -gt 3599 ]
do
        # At least 1 hour.
        let "hours += 1"
        let "sleeptime -= 3600"
done

# Subtract minutes.
while [ $sleeptime -gt 59 ]
do
        # At least 1 minute.
        let "minutes += 1"
        if [ $minutes -gt 59 ]
        then
                let "hours += 1"
                let "minutes -= 60"
        fi
        let "sleeptime -= 60"
done

seconds=$sleeptime


# Convert to xx:xx:xx format

if [ $hours -lt 10 ]
then
        tmp=$hours
        hours="0${tmp}"
fi

if [ $minutes -lt 10 ]
then
        tmp=$minutes
        minutes="0${tmp}"
fi

if [ $seconds -lt 10 ]
then
        tmp=$seconds
        seconds="0${tmp}"
fi

fulltime="${hours}:${minutes}:${seconds}"

${pmset} repeat ${command} ${type} ${days} ${fulltime}

Posted by slauncha at 04:55 PM | Comments (0)