Search code examples
linuxmacosbashcron

how to update homebrew with Cron on Mac os


I've been discvering some long lasting linux techs to help automate my daily work. I found cron to be very powerful if I can use it to check the updates of some packages I have on my system.

For example, I want to update my Homebrew everyday at 11pm. What I did is, with sudo crontab -u user -e, I opened up crontab in Vim. And I put following commands into it, to make updates for homebrew and send me an email.

Here's the code:

[email protected]
* 23 * * * brew update

and I save it to wait for magic happens. Instead of excuting this command, in the email I recieved, it says /bin/sh: brew : command not found

But when I type /bin/sh in terminal to open sh and type in brew update it will update the Homebrew

What did I do wrong with my crontab configuration?

Any help will be appreciated!


Solution

  • A cronjob is a good option, but I didn't want it to happen automatically. I found a script that will notify you if a new version of a formula installed on your Mac is available.

    I extended the script to not show pinned formulae in the notifier.

    I decided to use a launchd-agent for the cronjb, because this also runs if Mac is started later. Cron-jobs just run if your mac is already on at that time.

    For a comparison of cronjob vs launchd, I recommend reading this.

    Here's my configuration which runs every day at 10am and 3pm. The script, called by the agent, is located at /usr/local/bin/homebrew-update-notifier.

    <?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>EnableGlobbing</key>
        <false/>
        <key>Label</key>
        <string>homebrew.simonsimcity.update-notifier</string>
        <key>ProgramArguments</key>
        <array>
            <string>/bin/bash</string>
            <string>/usr/local/bin/homebrew-update-notifier</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/homebrew.simonsimcity.update-notifier.err</string>
        <key>StandardOutPath</key>
        <string>/tmp/homebrew.simonsimcity.update-notifier.out</string>
        <key>StartCalendarInterval</key>
        <array>
            <dict>
                <key>Hour</key>
                <integer>10</integer>
                <key>Minute</key>
                <integer>0</integer>
            </dict>
            <dict>
                <key>Hour</key>
                <integer>15</integer>
                <key>Minute</key>
                <integer>0</integer>
            </dict>
        </array>
    </dict>
    </plist>
    

    You will now be notified if a new update is available. Call brew upgrade if you feel outdated, or include it in the script.