How to keep Homebrew and XQuartz updated automatically

If you use Homebrew (and you should), eventually you may need to install XQuartz, "open-source effort to develop a version of the X.Org X Window System that runs on OS X. Together with supporting libraries and applications, it forms the X11.app that Apple has shipped with OS X since version 10.5."

The trouble is that brew won't install XQuartz, and it won't update it once it is installed, but it will complain if it isn't installed or is out of date. What I needed was a way to install it, if it isn't installed, or update it if it is out of date.

Also, brew is constantly being updated, which means that you should update it every day. Something you need to do every day? Do I even need to say "automation"? Wait. I said it... but you would have guessed it. I have faith in you.

There are three parts of this: 1) a shell script to update XQuartz, 2) a shell script to update brew, and 3) a launchd plist the run the script. You could combine #1 and #2 into one if you want, but I like keeping them separate.

launchd

Let's start with the easiest part: I want this to run automatically when my satellite quota is loosened, so I have a launchd plist to run my shell script at 3:03 a.m. every day:

Why 3:03 a.m.? Because my quota is loosened between 2:00 a.m. and 8:00 a.m., but I don't want to have to worry about Daylight Saving Time, so after 3:00 a.m. seemed like the best way to do that.

The Shell Script(s)

The updatebrew.sh script has two pieces. The first is di-xquartz.sh which will (d)ownload and (i)nstall XQuartz:

Another detail: if you want to be able to run that script without needing to enter your administrator password, you'll need to add this line to /etc/sudoers using sudo visudo:

%admin ALL=NOPASSWD: /usr/sbin/installer

The only time you should do this is if you are the only administrator user on your Mac, or if you understand the risks.

Next you need to run the two commands to update brew and any outdated brew utilities: brew update and brew upgrade, followed by brew doctor to make sure everything is OK. You want it simple? All you really need is this:

Note that this script includes the di-xquartz.sh as well as the brew commands.

That's pretty bare-bones, but it'll get the job done. Just be sure to check the log file. If you want something a little more (robust? complicated?), you could send the log to your iPhone using Pushover. As my Computer Science professor used to say, "the rest is left as an exercise for the reader."