Skip to content

Instantly share code, notes, and snippets.

@cpu
Created April 13, 2011 18:16
Show Gist options
  • Save cpu/918057 to your computer and use it in GitHub Desktop.
Save cpu/918057 to your computer and use it in GitHub Desktop.
A quick bash script to keep me from procrastinating
###
## Stay On Target!
## Daniel McCarney 2011
## https://binaryparadox.net
##
## Using an interactive countdown function found on the unix.com forums
## I created a shell function/alias that allows me to avoid procrastinating
## (if I procrastinate by writing code to stop procrastinating it equals out
## right?).
##
## Source this file and then use via:
## procrastinateLock 00:45:00 firefox "http://facebook.com" "http://twitter.com"
##
## All instances of the Firefox process will be killed, a 45 minute timer appears,
## and when it expires, a Firefox window to your time sinks opens.
##
# Taken from:
# http://www.unix.com/shell-programming-scripting/98889-display-runnning-countdown-bash+-script-2.html
function countdown
{
local OLD_IFS="${IFS}"
IFS=":"
local ARR=( $1 ) ; shift
IFS="${OLD_IFS}"
local PREFIX="$*" ; [ -n "${PREFIX}" ] && PREFIX="${PREFIX} > "
local SECONDS=$(( ${ARR[0]#0} * 3600 + ${ARR[1]#0} * 60 + ${ARR[2]#0} ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START
while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))
printf "\r%s%02d:%02d:%02d" \
"${PREFIX}" $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
sleep 1
done
echo " "
}
#Kill all instances of Arg 2, run a countdown (duration set in arg 1)
#and then invoke Arg 2 using all the remaining arguments received.
function procrastinateLock
{
#Comment following to not frag whatever command you're locking.
killall $2
countdown $1
#How many args received?
args=$(($#))
#Slice args from 2 onwards
argsArray=${@:2,$args}
$2 $argsArray&
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment