Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Buadhai

macrumors 65816
Original poster
Jan 15, 2018
1,121
436
Korat, Thailand
I'd like to run a shell script every night using launchd when the laptop (M1 MBA) lid is closed.

I can ssh in to the machine when the lid is closed and run the script from the command line. But running via launchd doesn't seem to work.
 

f54da

macrumors 6502a
Dec 22, 2021
504
186
launchd does not work when sleeping, it will only be run on next wake. Can you clarify on how you're ssh'ing into the machine, the machine would drop network connectivity unless you're using wake-over-lan or equivalent.

Note that it's possible that darkwake/powernap gives you some opportunities to run things at a periodic interval, although off the top of my head I don't believe there's any api to actually schedule when this happens. (I also don't know if this is something that's even schedulable or whether it just decides to wake once an hour. Reading the xnu source would tell you but I'm too lazy to do that.)

Another option is scheduling a `pmset wake` but it's not clear if this works when lid closed.
 

Buadhai

macrumors 65816
Original poster
Jan 15, 2018
1,121
436
Korat, Thailand
launchd does not work when sleeping, it will only be run on next wake. Can you clarify on how you're ssh'ing into the machine, the machine would drop network connectivity unless you're using wake-over-lan or equivalent.
That's what I was afraid of. And, yes, I'm using wake for network access to log on to the closed MBA.

However, I do have my iMac sleep and it will still run launchd stuff in the middle of the night. Is it possible that it's just the closed lid and not the sleep that causes launchd to choke?
 

f54da

macrumors 6502a
Dec 22, 2021
504
186
>Is it possible that it's just the closed lid and not the sleep that causes launchd to choke?

Yeah that's possible, perhaps launchd is actually smart enough to handling scheduling a wake via pmset for you. But if the lid is closed, that usually overrides everything (e.g. even if you normally set a keep awake pmset assertion, closing the lid overrides that and will force the laptop to sleep).

There likely might be a way to get around this (I know that you can keep the laptop awake even with lid closed via an undocumented assertion) but you'd need to poke around in the IOPMPrivate source code dumps and XNU source apple released.
 

Buadhai

macrumors 65816
Original poster
Jan 15, 2018
1,121
436
Korat, Thailand
Turns out I can't get scripts to run via launchd even when the lid is open and the machine is awake. Worse, than that, my iMac, which has been running scripts for a very long time, won't run any new script that I write and try to run with launchd. I'm sure that I'm just making a stupid mistake. Thread here:

Launchd runs old scripts but won't run new scripts
 

Buadhai

macrumors 65816
Original poster
Jan 15, 2018
1,121
436
Korat, Thailand
Turns out launchd will run scripts when the lid is closed in the middle of the night. Oddly, it will run this command:

Code:
curl -sS https://api.prowlapp.com/publicapi/add  \
-F apikey=$apikey -F application=$(hostname) \
-F event="Prowl Test"  -F description="Testing prowl with laptop closed" \
>> $outfile 2>&1

But not this:

Code:
echo $now "Hatchet Mail Test" | /usr/bin/mail -s \
"Email test with lid closed" $email >> $outfile 2>&1
 

Buadhai

macrumors 65816
Original poster
Jan 15, 2018
1,121
436
Korat, Thailand
So, the final answer is: ีYes, launchd will run a script when the laptop lid is closed."


IMG_0670.jpeg
 
  • Like
Reactions: Basic75 and chown33
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.