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

40 lines
1 KiB
Bash
Executable file

#!/usr/bin/env dash
# YES_ZSH
# YES_BASH
# YES_DASH
minutemaid() { ### @-
### return 0 if the current minute is divisible by a number.
### note that a minute is relative to the seconds since the epoch, not the minute of the hour.
###
### ```
### # crontab usage:
### * * * * * minutemaid 9 ~/work/do_my_bidding # runs every nine minutes
### ```
local offset=0 name
while getopts 'o:h' name; do
case $name in
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;;
esac
done
shift $((OPTIND-1))
local interval="${1:?no interval specified}"
shift
local sec="$(date +%s)"
local min="$((sec/60+offset))"
local mod="$((min%interval))"
if [ $# -gt 0 ]; then
[ $mod -ne 0 ] || "$@"
else
[ $mod -eq 0 ] && return 0 || return 1
fi
}
[ "${SOURCING:-0}" -gt 0 ] || minutemaid "$@"