Keeping up with the brews

Keeping up with the brews

Adham 2 min read

I have a confession. I was lazy with Homebrew updates for months.

I kept saying, “I will update it tomorrow,” and tomorrow never came. So one day I decided to fix this properly and write a script.

The idea looked smart at first

I wrote a Bash script that checks if brew has updates, then updates my shell startup state so I can see a message in the terminal.

The interesting part was not the message. The interesting part was this optimization:

Instead of running brew outdated every time I open a terminal, I made it run at most once per day.

I stored the result in a cache file. So on any given day, only one terminal session pays the slow cost. Every other session reads the cached result.

It actually worked very well.

# simplified idea
BREW_CACHE_FILE="$HOME/.cache/brew-outdated.txt"
BREW_CACHE_DATE="$HOME/.cache/brew-outdated-date.txt"
TODAY="$(date +%F)"

if [[ -f "$BREW_CACHE_DATE" ]] && [[ "$(cat "$BREW_CACHE_DATE")" == "$TODAY" ]]; then
  # use cached result
  cat "$BREW_CACHE_FILE"
else
  # refresh once per day
  brew outdated > "$BREW_CACHE_FILE"
  echo "$TODAY" > "$BREW_CACHE_DATE"
fi

Neat, right?

Then I realized I over-engineered it

After running this setup for more than a month, it finally hit me.

Linux and Unix systems solved this pattern a long time ago. It is called cron.

My solution was not wrong. It was just heavier than needed.

The simpler path

The real solution is to use a scheduled background job that runs on schedule, writes the result to a file, and my shell just reads that file quickly.

I would say typically you would use cron.d, but on macOS everything is about launchd instead, cron is not enabled by default.

Now terminal startup stays fast, logic is simpler, and the system is easier to reason about. Then from shell startup, I only read the file if it exists.

What I learned

Sometimes we over-engineer because building clever things feels productive.

But simple, boring tools are often the best fit.

For me, this was a good reminder: before inventing a custom workflow, check if the operating system already gives you the exact primitive you need.

References