1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-14 14:09:03 -08:00
rc/sh/minutemaid

30 lines
668 B
Bash
Executable file

#!/bin/zsh
# crontab usage:
#* * * * * minutemaid 9 cd repo && git pull # runs every nine minutes
local offset=0 opt=
while getopts 'o:h' opt; do
case $opt in
o) offset="$OPTARG";;
?) local fd=0
[ $opt = h ] && fd=0 || fd=2
echo -E "usage: $0 [-o offset] {interval} [{command} [{args...}]]" >&$fd
[ $opt = h ] && return 0 || return 1;;
esac
done
shift $((OPTIND-1))
local interval="${1:?no interval specified}"
shift
local sec="$(date +%s)"
let min=sec/60+offset
let mod=min%interval
if [ $# -gt 0 ]; then
local cmd="${1}"
shift
[ $mod -eq 0 ] && "$cmd" "$@"
else
[ $mod -eq 0 ] && return 0 || return 1
fi