Sudo Launchctl stop service on sleep trigger - make your Mac Sonoma sleep with Samba share enable

The reason need to sleep samba before sleeping on Sonoma because the default behavior of Sonoma 14.4 or after would NOT sleep if you have samba share file sharing enabled. I was trying to stop the smbd service when the Mac sleep with something like this

/bin/launchctl stop com.apple.smbd

I read about this post

However, the sudoer never works for me.
I login as root and type visudo and add this line with "myUsername"

However, when I run the command

sudo /bin/launchctl stop com.apple.smbd

It give me this error right away

"Not privileged to stop service."

Ok...after playing with this for a couple hours...here is the solution.

  1. You have to disable SIP. This is some new security setting which keep you from stopping any Apple daemon via launchctl in Sonoma 14.4 or anything after. Disabling and Enabling System Integrity Protection | Apple Developer Documentation

  2. You need to add this line as root by typing "visudo"
    yourUsualLoginUsername ALL=NOPASSWD: /bin/launchctl stop com.apple.smbd

  3. You have to use sleep a couple second before you run sudo /bin/launchctl stop com.apple.smbd in "Execute Shell Script" look something like this

The reason you need to disable SMB before sleeping because the default behavior of MacOS Sonoma File sharing samba share will keep your Mac from sleeping as detect by Sleep Aid Your computer will NOT sleep if you enable samba share on Sonoma, I don't know why but I use an external meter to track the power consumption, it never sleep if you have File sharing Samba enabled on Sonoma and you have a Windows PC to mount the network share as a drive on Windows.

The power consumption on Mac Studio M1 should use about 9-10w when sleeping (external metered power adapter measure). Anything higher than this indicate your Mac is not sleeping.

You may already know this, but for the sake of others who may not know this:

I'm also a little concerned about your use of NOPASSWD but I don't really understand the security implications of that.

that's why you have to specify only 1 action can be done without password

yourUsualLoginUsername ALL=NOPASSWD: /bin/launchctl stop com.apple.smbd

which is "/bin/launchctl stop com.apple.smbd"

All other action still need a password.

Regarding SIP, there is no other way you can work around this if you want to stop Apple's own daemon.

I mean...you don't have to do this. However, if you want to properly sleep your Mac on Sonoma with Samba share enabled, I can't find another solution.

Thanks for clarifying that. It relieves my concerns by about 5%.

And I think you could play with Sleep Aid - View a Mac's sleeping habits, solve issues, improve battery life, stop sniffers , this software actually tells you if your mac is sleeping at all......

You will be amazed a lot of default setting on Sonoma just don't sleep your Mac.......at all.

It shouldn't be a deal breaker if no password only allow a particular user able to run one single command.......... which is turning off samba share.

1 Like

Fair point. But my Mac is a desktop, not a laptop, so I don't think this software is particularly useful for me.

The website says "Apple Verified. No malicious software." But the link takes me to a generic Apple site which isn't very comforting. I'm guessing that what they really mean is that they are a developer that has been identified by Apple as being a trusted developer, which is probably verifiable during the installation of the software, but I'm not going to install it. However I did do five minutes of research into the developers of that software and I don't detect anything fishy about them.

Are you sure? Or do you, perhaps, have your machine set to "Wake on network access" plus something else on the network that's polling the share?

Anyways, turn SIP back on (for your own protection!) and then update sudoers and your macro to use the following:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbd.plist

And, of course:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.smbd.plist

...when you want to restart smbd.

Tested on Sonoma 14.5

Nothing is turned on, I basically turn off any thing that will wake up the mac besides powernap. I use an external metered adapter something like this to check the actual power usage in real time. The power adapter measure how much power drawn by the Mac in real time.

I have a PC mount the samba share of the Mac at all time. If I have access the samba share once on the PC, even if you sleep the Mac it will keep using 20w power and I can still ping the Mac. That means it's not sleeping.

When I kill the smbd before putting the Mac to sleep, the power consumption drop to about 9-10w and I can't ping the Mac.

Just to confirm your sudo launhctl command works on Sonoma.

That's the actual problem -- it's not that your Mac won't sleep because Sharing is turned on, but because a client is connected.

It would be better to have the client disconnect first so that your Mac sleeps "normally". If you can't do that then yes, turning off smbd will "solve" the problem -- but beware of data loss since you are force-disconnecting the client...

Yea, I mounted the samba share on "Y:" Drive on Windows. When I sleep the Mac, I will not access the drive anyway so it should be ok.

Just FYI, MacPort version of samba will sleep the smbd correctly on Sonoma. It's something do with the version of smbd comes with Sonoma. I haven't tried Homebrew version yet so I don't know if that works.