Mac Tips — Homebrew auto update

Mac 的套件管理軟體 Homebrew 使用十分方便。不過因為太無痕了,時常會忘記更新,有時要安裝新套件的時候需要花很多時間等更新完成,便想自動化更新。原先想用 cron 自動執行,不過發覺蘋果官方建議使用 launchd,如此在 ~/Library/LaunchAgents/ 中新增檔案即可。

簡單上網搜尋後,發現有其他人有同樣需求,放在 github 上頭。參考此專案自行修改為 ~/Library/LaunchAgents/sh.brew.HomebrewUpdate.plist

<?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>sh.brew.HomebrewUpdate</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>TERM</key>
        <string>ansi</string>
    </dict>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/brew</string>
        <string>update</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>00</integer>
        <key>Minute</key>
        <integer>24</integer>
    </dict>
</dict>
</plist>

如此在每日凌晨零點 24 分會自動更新,可用 launchctl list | grep brew 查看執行結果。