Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Last active March 26, 2020 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonmichael/12de3fc72fcc8493a5d7f6b8a6628315 to your computer and use it in GitHub Desktop.
Save simonmichael/12de3fc72fcc8493a5d7f6b8a6628315 to your computer and use it in GitHub Desktop.
cron job to autocommit journal & related files
# simon's crontab, started 20190104.
# Edit this file, then: make cron-install (installs to /usr/lib/cron/tabs/simon)
# Don't edit installed crontab (crontab -e); if I forgot: make cron-sync
# Show installed crontab: make cron-show
# Show difference from this file: make cron-diff
# /usr/lib/cron/tabs/simon - simon's crontab, created 20190104
# make cron-show
# make cron-install, in iTerm (not emacs shell)
#
# alternatives:
# anacron - not available on mac
# launchctl - can't make it work on mojave
# Lingon X - launchctl manager
# LaunchControl - launchctl manager
#
# cron jobs run only if system is awake when they come due
# use pmset to ensure a nightly awake window (make nightly-wake-start)
PATH=/Users/simon/finance/bin:/Users/simon/.local/bin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
LEDGER_FILE=/Users/simon/finance/journal
# minute hour mday month wday command
# log frequently to show when cron is active
* * * * * date >>/Users/simon/finance/cron.log
# Nightly wake window is 0359-0415, see Makefile -> mac-nightly-wake-(start|stop|show).
# During nightly wake:
# fetch new transaction data
00 04 * * * make -C/Users/simon/finance -s csv import
# autocommit financial files
02 04 * * * make -C/Users/simon/finance -s autocommit
# daily financial report
# XXX uses freshly downloaded txns that may need more cleanup
# XXX goes to fastmail, too public
#03 04 * * * make -C/Users/simon/finance -s daily-reports | mail -s "daily report" simon
# weekly client time reports on saturday
#04 04 * * 6 PATH=$PATH:~simon/finance/bin mailclienttimes
# cron
# install ./crontab as the active crontab
cron-install:
@crontab ./crontab
# overwrite ./crontab with the active crontab
cron-sync:
@crontab -l >./crontab
# show the active crontab
cron-show:
@crontab -l
# show the difference between ./crontab and the active crontab
cron-diff:
@crontab -l > .crontab.tmp
@-diff ./crontab .crontab.tmp
@rm -f .crontab.tmp
# schedule nightly wake window for cron (cf crontab)
mac-nightly-wake:
sudo pmset repeat wakeorpoweron MTWRFSU 03:59:00 sleep MTWRFSU 04:13:00
pmset -g sched
mac-nightly-wake-cancel:
sudo pmset repeat cancel
pmset -g sched
mac-nightly-wake-show:
pmset -g sched
# Perform any auto-commits that are due, in the proper order.
autocommit: \
autocommit-time \
autocommit-txns \
autocommit-misc
# Auto-commit any timelog changes.
autocommit-time:
@echo `date` $@ >>$@.log
@( git commit -qm 'time (auto)' $(TIMEDOT) | grep -E '(^\[|file changed)' ) >>$@.log \
|| true
# Auto-commit any transaction journal changes.
autocommit-txns:
@echo `date` $@ >>$@.log
@( git commit -qm 'txns (auto)' $(JOURNAL) | grep -E '(^\[|file changed)' ) >>$@.log \
|| true
# Auto-commit any remaining dirty files if nothing much has changed in the last hour
# (excluding journal, timelog, log files, git files..)
# Run this after the more specific autocommits above.
autocommit-misc:
@echo `date` $@ >>$@.log
@[[ -z $$(find . -newermt '60 minutes ago' \! -regex '\./\.git.*' \! -name "*.log" \! -name $(TIMEDOT) \! -name $(JOURNAL) ) ]] \
&& ( git commit -am '(auto)' | grep -E '(^\[|file changed)' ) >>autocommit.log \
|| true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment