Skip to content

Instantly share code, notes, and snippets.

@sisso
Last active June 8, 2022 19:45
Show Gist options
  • Save sisso/23e3b6903f61c0c4df55862abc83ba77 to your computer and use it in GitHub Desktop.
Save sisso/23e3b6903f61c0c4df55862abc83ba77 to your computer and use it in GitHub Desktop.
crab script ro run things daily one hour after the computer boot, just once a day. You need run this on user cron on every hour
#!/bin/bash
set -euo pipefail
# set -x
dir="${HOME}/.daily-scheduler"
log="${dir}/log"
mkdir -p "${dir}"
now=$(date -I)
log()
{
echo "$*" >> "${log}"
}
if grep -aq "$now complete" "${log}"; then
# log "$now already executed, ignoring"
true
else
if grep -aq "$now scheduled" "${log}"; then
# check if is current time
next="$(grep -oaP "$now scheduled \K\d+" "${log}")"
hour="$(date +%-H)"
if [[ hour -ge next ]]; then
log "$now running"
$*
log "$now complete"
else
log "$now waiting for ${next}"
fi
else
# schedule next hour
hour=$(($(date +%-H) + 1))
log "$now scheduled $hour"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment