1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 05:49:02 -08:00
rc/sh/minutemaid

35 lines
847 B
Text
Raw Normal View History

#!/usr/bin/env dash
2015-03-05 06:49:03 -08:00
# crontab usage:
#* * * * * minutemaid 9 ~/work/do_my_bidding # runs every nine minutes
2021-07-29 00:37:35 -07:00
# YES_ZSH
# YES_BASH
# YES_DASH
2015-03-05 07:15:09 -08:00
2021-07-29 00:37:35 -07:00
minutemaid() {
local offset=0 name
while getopts 'o:h' name; do
case $name in
2021-07-29 00:37:35 -07:00
o) offset="$OPTARG";;
?) local fd=0
[ $name = h ] && fd=1 || fd=2
printf "%s\n" "usage: $0 [-o offset] {interval} [{command} [{args...}]]" >&$fd
[ $name = h ] && return 0 || return 1;;
2021-07-29 00:37:35 -07:00
esac
done
shift $((OPTIND-1))
2015-03-05 06:49:03 -08:00
2021-07-29 00:37:35 -07:00
local interval="${1:?no interval specified}"
2015-03-05 07:15:09 -08:00
shift
2021-07-29 00:37:35 -07:00
local sec="$(date +%s)"
local min="$((sec/60+offset))"
local mod="$((min%interval))"
2021-07-29 00:37:35 -07:00
if [ $# -gt 0 ]; then
[ $mod -ne 0 ] || "$@"
2021-07-29 00:37:35 -07:00
else
[ $mod -eq 0 ] && return 0 || return 1
fi
}
[ "${SOURCING:-0}" -gt 0 ] || minutemaid "$@"