1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-18 17:53:23 -07:00
rc/sh/minutemaid
2021-07-29 00:37:35 -07:00

36 lines
842 B
Bash
Executable File

#!/usr/bin/env zsh
# crontab usage:
#* * * * * minutemaid 9 cd repo && git pull # runs every nine minutes
# YES_ZSH
minutemaid() {
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
}
[ "${SOURCING:-0}" -gt 0 ] || minutemaid "$@"