I have a cron job that upgrades my Homebrew softwares periodically. Sometimes it runs into the famous directory ownership and permission issue for Homebrew. I'm trying to include some commands in the cron job to always fix them before upgrading. Here is what I've come up:
## fix owner of files and folders recursively
sudo chown -R $(whoami):wheel $(brew --prefix)/* /opt/homebrew-cask
sudo chown -R $(whoami) $HOME/Library/Caches/Homebrew
fix read/write permission of files and folders recursively
chmod -R u+rw $(brew --prefix)/* /opt/homebrew-cask $HOME/Library/Caches/Homebrew
fix execute permission of folders recursively
find $(brew --prefix) /opt/homebrew-cask "$HOME/Library/Caches/Homebrew" -type d -exec chmod ug+x '{}' ;
Does it look up-to-date (macOS 13.1, Homebrew 4.2.10-9-g7848bd3)? Safe? Good practice?
Potential issues:
chown: /usr/local: Operation not permitted
Update: For commenters asking about the motivation or necessity of the question:
- The source of the question is the same situation as described in Are my permissions for /usr/local/ correct? (Note that both the question and the answer are very old though.)
- But I want to add a (up-to-date, best designed or safest) solution to this recurring issue in a script that's run in a cron job so I don't need to manually do it from time to time.
- BTW, searching the keywords suggest there are people asking and answering about the issue all the time till recent: https://www.google.com/search?q=macOS+Homebrew+"chown"+after%3A2023
brew doctornot provide sufficient help to resolve the issues, instead of tinkering with the permissions yourself? – Saaru Lindestøkke Feb 27 '24 at 11:59/opt, and fix the actual problem. The original problem under/usr/localwas due to Homebrew deciding they owned that directory and its subdirectories. – Marc Wilson Feb 27 '24 at 16:27/opt/homebrewis the standard location for an ARM copy of Homebrew. Intel Homebrew still lives in/usr/local– Marc Wilson Feb 27 '24 at 23:59brewin a hands-off fashion. Occasionally the upgrade process will throw up manual interventions which need to be read and understood thoroughly, or you might end up with non-working formulae. Those interventions, of course, are completely the decision of the formula maintainer. All-round dubious practise, whether it bebrew upgrade,apt upgrade, etc. I hope you have good backups taken as regularly as you schedule this script. – Andy Griffiths Feb 28 '24 at 00:31/opt? – Marc Wilson Feb 28 '24 at 04:07