Skip to content

Instantly share code, notes, and snippets.

@ekimekim
Created May 29, 2013 06:11
Show Gist options
  • Save ekimekim/5668278 to your computer and use it in GitHub Desktop.
Save ekimekim/5668278 to your computer and use it in GitHub Desktop.
Run a command at a certain time. Simpler than at(1) - it creates a background job in the current shell environment.
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "USAGE: $0 TIME COMMAND {ARGS}"
echo "Run COMMAND with ARGS at specified TIME."
echo "TIME may be given in any form understood by date(1)"
exit 2
fi
duestr="$1"
shift
due=`date -d "$duestr" +%s`
if [ ! "$due" ]; then
echo "Error: Could not understand due time."
exit 1
fi
now=`date +%s`
interval=$((due - now))
if [ "$interval" -lt 0 ]; then
echo "Error: $durstr is $((-interval))s in the past."
exit 1
fi
( sleep "$interval" && "$@" ) &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment