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.

  • You can do this in ~/.bash_profile file. You'll need to check if only 1 Terminal session is open and then run brew update. Check stackoverflow.com/questions/6172663/… for determining number of Terminal windows. – Mateusz Szlosek Sep 19 '15 at 16:57
  • That is the number of the terminals that are open when the script is executed or the number of terminals opened in general? – g_rmz Sep 19 '15 at 17:09
  • That's the number of Terminals opened (windows + tabs) when the script is run. ~/.bash_profile is automatically executed at the start of Terminal session. This check is needed if You need to run brew update at the first start. If You want to run this every time new window is opened, skip this step and run brew update in ~/.bash_profile. – Mateusz Szlosek Sep 19 '15 at 17:12
  • 1
    It will make opening a Terminal tab very slow – mmmmmm Sep 19 '15 at 17:17
  • Ok thank you! :) It will decrease the terminal's performance very bad? – g_rmz Sep 19 '15 at 17:21

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



$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.

brewup &
| improve this answer | |
  • 2
    +1 I agree that it's better to use tome sort of launchclt instead of running at every start of Terminal. Additonally it's Terminal independent (for iTerm users). – Mateusz Szlosek Sep 19 '15 at 18:48
  • 1
    This is brilliant, thank you! :) How about crontab instead of Lingon? – g_rmz Sep 19 '15 at 18:53
  • 3
    @g_rmz There's nothing wrong with crontab and it's easy to know. If it works, run with it. The benefits of launchd over cron is that it's more resilient, more power friendly, and handles sleep / missed intervals more naturally. – bmike Sep 19 '15 at 18:59
  • Thanks for at great script. Just out of curiosity - what's the -t option? – avriis Mar 31 '18 at 8:21
  • 1
    Hey @dahved. See the man page for logger for the dry version of the t flag. I use it so I can find these messages in the sea of logs with an easy grep or log command and predicate/search term. – bmike Mar 31 '18 at 13:39

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).

| improve this answer | |

I prefer to update Homebrew on start-up. I have a script Update Homebrew.sh in ~/Library/Scripts:


for cmd in update upgrade cleanup\ -s; do
  brew $cmd

This script is run on start-up using launchd. For that, I have Update Homebrew.plist in ~/Library/LaunchAgents:

<?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">
        <string>Update Homebrew</string>
            <string>/Users/Daan/Library/Scripts/Update Homebrew.sh</string>

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!

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .