Question

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:

MAILTO=myemail@foo.com
* 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!

Was it helpful?

Solution 2

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.

OTHER TIPS

Cron doesn't have your PATH defined, make sure you always call commands with the full path, which is probably /usr/local/bin/brew update

This is considered good practice to keep unwanted/unexpected commands from running. If someone put a malicious script called 'brew' somewhere else in your path, but before /usr/local/bin, it would get called instead.

There's probably something wrong with your bash configuration. Make sure you set the PATH so that it contains the directory that brew is in. You may have it set in your ~/.profile but make sure it's also set in your ~/.bashrc. Alternatively, you could set it in your crontab in the same way you set MAILTO

Personally the way I handle this is:

  • Download CronniX (it's discontinued but still works, e.g. brew cask install cronnix)

  • Setup the job within the UI via /usr/local/bin/brew update to run every hour.

Let it tick over in the background!

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top