How to use sudo to run brew services stop mysql as non root user in script running as root

Hi,


This is a bit of a curly one. I need to run the brew services command to stop mysql in a script running as root. The problem is that command should not be run as the root user and so I am trying to use the -u and -i flags on the sudo command for:

sudo -u <username> -i brew services stop mysql@5.7

Unfortunately, this results in the output of:

Error: Service mysql@5.7 is not started.

This is true as far as the root user is concerned as #brew services will produce:

mysql@5.7 stopped

However, when you drop down to the user where the mysql@5.7 service was started from, it is still running.

What am I doing wrong here? Can anyone help? I’ve posted this on Apple Stack Exchange with no response so far :/

Thanks,

GAM

Mac mini, macOS 10.13

Posted on Jan 21, 2022 4:06 AM

Reply
7 replies

Jan 21, 2022 4:18 PM in response to VikingOSX

Fair enough, but as we say here in Oz, "Yeah nah". I think this is fundamentally a Mac problem because this boils down to just launchctl being able to start a service (mysql) as a non-root user whilst being *called* as root. It doesn't matter that it's brew doing the calling (of launchctl) or that it's mysqld_safe being called (by launchctl). I wouldn't go to the mysql community for this, but I might go to brew's as I could be mis-understanding what's happening between brew & launchctl.


Thanks,


GAM

Jan 23, 2022 6:41 PM in response to gamcall64

Why do you need to reference Brew here. If you started a service in macOS, then just treat it as any other service. Except for installing a different version via Brew, the only thing that matters is referencing the correct service you want to stop. Brew should be irrelevant except for installing/uninstalling/updating the utility.


If this is actually part of Brew itself (i.e. controlling Brew itself as opposed to you installing mysql yourself), then it would be better to contact the HomeBrew community and developer for assistance as it would be a Brew issue.



Jan 26, 2022 10:17 PM in response to HWTech

I reference Brew because the service was started under Brew.

If I try and directly use launchctl to unload it with something like:


#sudo -u user -i launchctl unload /usr/local/Cellar/mysql@5.7/5.7.37/homebrew.mxcl.mysql@5.7.plist


Then I get:

Could not find domain for


I have tried messing around with various combinations of the -S & -D flags for launchctl without success. Perhaps you could suggest something else to try?


Regards,


GAM


Jan 27, 2022 4:10 PM in response to gamcall64

How are you loading the service? Is the service even started using the Homebrew version? If the Homebrew service version has not been loaded yet, then I would expect that error when trying to stop a service that is not loaded.


I'm not really familiar with starting/stopping services on macOS. Hopefully a more knowledgeable contributor can chime in with more ideas.

Jan 28, 2022 4:24 PM in response to HWTech

The service is being (successfully) loaded (& unloaded) under brew, viz:


brew services start mysql@5.7


That's (I think) just a uniform CLI brew provides to launchctl for software to run as a service; i.e. under the Mac's "one program to rule them all"; launchctl. The corresponding unload is, as you'd expect:


brew services stop mysql@5.7


Thanks,


GAM

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to use sudo to run brew services stop mysql as non root user in script running as root

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.