bin/track-disk-usage: periodically log some directory sizes to a tsv file,

so we can keep track of where our disk space is going.
1 files changed, 49 insertions(+), 0 deletions(-)

A => bin/track-disk-usage
A => bin/track-disk-usage +49 -0
@@ 0,0 1,49 @@ 
+#!/bin/sh
+
+LOCKFILE="$HOME/.cache/track-disk-usage.lock"
+
+DATAFILE="$HOME/.cache/track-disk-usage.tsv"
+
+# Passed to sleep(1)
+LOG_INTERVAL="30m"
+
+log_dir_sizes() {
+    cd "$HOME" || exit 1
+    du -s \
+        ./* \
+        $(hidden) \
+        proj/* \
+        .local/* \
+        .thunderbird/* \
+        .cache/* \
+        lib/miniconda3/envs/* \
+        2>/dev/null \
+    | sed "s/\$/	$(date '+%Y-%m-%d %H:%M:%S')/" \
+    > "$DATAFILE"
+}
+
+work_loop() {
+    while true; do
+        log_dir_sizes
+        sleep $LOG_INTERVAL
+    done
+}
+
+main() {
+    check_lock="$1"
+    if test "$check_lock" = "--lock_was_obtained"; then
+        # We got a lock, do the work.
+        work_loop
+    else
+        # If we can get a lock, rerun this script with the 'we got a lock'
+        # flag.
+        flock --nonblock "$LOCKFILE" "$0" --lock_was_obtained \
+        || {
+            echo Could not obtain lock on "$LOCKFILE".
+            echo 'track-disk-usage is already running.'
+            exit 1
+        }
+    fi
+}
+
+main "$1"