i'm currently using the Homebrew package manager and my question is: is possibile to write a bash's script in order to execute
brew update and eventually
brew upgrade whenever opening a shell for the first time? I'm using iTerm at the moment.
This is very easy to do.
For efficiency (and cool factor), I would use a tool like Lingon to launch this script periodically using launchctl/launchd instead of each time you start a shell. On my MacBook, it takes 3 seconds to update the second time (no work done, DNS cache set, etc...) and it take 10 second to run the first time (no work done) or 15+ seconds if a package needs to be downloaded or compiled.
Perhaps once a day or once an hour - running in the background would be sufficient given those times to execute?
You could make a simple script
/usr/local/bin/brewup that calls brew in turn and logs the results to the system log. If you don’t like chasing logs in the centralized log store, “teeing” them to a text file works well, too.
#!/bin/bash brew=/usr/local/bin/brew logger=/usr/bin/logger $brew update 2>&1 | $logger -t brewup.update $brew upgrade 2>&1 | $logger -t brewup.upgrade $brew cleanup 2>&1 | $logger -t brewup.cleanup
I just call the brewup when I'm about to go make tea or when I get started and let it run in the background.
There is a tool called homebrew-autoupdate which will do this for you. It can automatically run
brew update in the background every 24 hours (configurable) to ensure that you always have fresh homebrew data when you go to install/upgrade packages.
To install it run
brew tap domt4/autoupdate and
brew autoupdate --start 43200 to configure it to autoupdate every 12 hours (43200 seconds).
You might want to ask yourself: "Do I really want to auto-update applications, which the applications that I build myself depend upon?"
My interpretation of their reasoning: "The risk of malicious code slipping through the opensource auditing process weighs more than the incidental need for a speedy update."
If this is your line of reasoning, you like to be in control of software updates, and you care a lot about homebrew packages being up-to-date, you are probably better off adding a line to the bottom of
.bashrc (or the rc file of your favorite shell) asking:
This will confront you, in every new terminal, with the outdated packages. When your annoyance reaches the limit (you feel ready to update), you'll be happy to do the necessary.
I prefer to update Homebrew on start-up. I have a script
Update Homebrew.sh in
#!/usr/local/bin/bash for cmd in update upgrade cleanup\ -s; do brew $cmd done
This script is run on start-up using
launchd. For that, I have
Update Homebrew.plist in
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>Update Homebrew</string> <key>ProgramArguments</key> <array> <string>/Users/Daan/Library/Scripts/Update Homebrew.sh</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
Note that it might not update reliably when you, say, have a MacBook and only open and close the lid. However, it works well for my iMac that I regularly shut down and start up. Let me know if it works!
You can use cron to simplify this.
Find your brew installation location with
Mine was in the default location:
crontab -e add this line to the editor (update with your brew location):
0 9 * * * /usr/local/bin/brew update && /usr/local/bin/brew upgrade && /usr/local/bin/brew cleanup
The above will update brew, upgrade formulae and casks, and then clean up - every morning at 9am.