1

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:

  1. chown: /usr/local: Operation not permitted

Update: For commenters asking about the motivation or necessity of the question:

Meng Lu
  • 243
  • 1
    What exactly is your question here? How do the commands relate to the issue you list? – nohillside Feb 27 '24 at 06:17
  • 2
    Why do you find it necessary to change ownership/permissions of Homebrew files? If you try to update manually, do you still have to do this – Allan Feb 27 '24 at 07:00
  • You also have to be ready for some upgrades needing admin's password. – Gilby Feb 27 '24 at 08:31
  • 2
    Does brew doctor not provide sufficient help to resolve the issues, instead of tinkering with the permissions yourself? – Saaru Lindestøkke Feb 27 '24 at 11:59
  • 1
    I think I'd be more concerned about what might be changing permissions under a non-standard directory like /opt, and fix the actual problem. The original problem under /usr/local was due to Homebrew deciding they owned that directory and its subdirectories. – Marc Wilson Feb 27 '24 at 16:27
  • @MarcWilson: That's the reason why I was thinking about doing this as part of my standard homebrew update script. Using /opt/homebrew-cask instead of /opt is still not safe enough because /opt is not a standard place for Homebrew, right? About /usr/local, I never understood why the ownership/permission issue would pop up every once in a while, but Internet always suggested doing a chown on it. And for the chown command, I don't know what's the safest or the most well designed version is. That's why I'm asking it here. Just don't want to do a Google search every time and pick a random solution. – Meng Lu Feb 27 '24 at 21:40
  • @MengLu /opt/homebrew is the standard location for an ARM copy of Homebrew. Intel Homebrew still lives in /usr/local – Marc Wilson Feb 27 '24 at 23:59
  • @MarcWilson: I was just reading about that difference in another web site. I verified. My computer is an Intel one. – Meng Lu Feb 28 '24 at 00:06
  • 1
    I would suggest that it's not a good idea to update brew in 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 be brew 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
  • @MengLu Was there a reason you chose to install Homebrew in a non-standard prefix, or is your copy of Homebrew not actually in /opt? – Marc Wilson Feb 28 '24 at 04:07
  • What makes you think that the old Q&A isn‘t valid any longer? Have you tried the solution described there, did it work (and if not, how did it fail)? Right now I don‘t see any indication that this here isn‘t a duplicate. – nohillside Feb 28 '24 at 05:55
  • @MarcWilson: Intel one uses /usr/local. – Meng Lu Feb 28 '24 at 19:29

0 Answers0