MacOS shutdown hook

Joined
Sep 8, 2017
Messages
5
Reaction score
0
Points
1
Trying to find a way to run a shutdown hook on mac from a command-line background process.

Tried using NSWorkspaceWillPowerOffNotification, which was never called. Also tried kIOMessageSystemWillPowerOff event and that did not run either.

Anyone has any idea?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
You haven't provided full context of where you are looking to execute the shutdown, but like all Unix system the command below will shutdown the Mac..

Code:
sudo shutdown -h now
 
OP
Y
Joined
Sep 8, 2017
Messages
5
Reaction score
0
Points
1
I am running this on mac, but the idea is not to shut down but

You haven't provided full context of where you are looking to execute the shutdown, but like all Unix system the command below will shutdown the Mac..

Code:
sudo shutdown -h now

I am running this on mac, but the idea is not to shut down but to be able to run logic upon shutdown
 

pigoo3

Well-known member
Staff member
Admin
Joined
May 20, 2008
Messages
44,210
Reaction score
1,418
Points
113
Location
U.S.
Your Mac's Specs
2017 15" MBP, 16gig ram, 1TB SSD, OS 10.15
I am running this on mac, but the idea is not to shut down but to be able to run logic upon shutdown

This is a bit unclear (at least to me). Are you saying that you want your Mac computer to do this series of events:

First = Start the shutdown program.
Second = run Logic
Third = Shut down computer

If this is not correct...please clarify.

- Nick
 
OP
Y
Joined
Sep 8, 2017
Messages
5
Reaction score
0
Points
1
That is correct.
To be more presise, I want to log each shut down time to a log file
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
OK, I see what you are trying..you said background process, if that is a shell script, one possibility is to trap the script being killed and then run a function where you logic resides. However, this is not a deterministic way of handling the case, i.e., the system is killing all running scripts and expects that to happen quickly and you can't too long or delay the shutdown, so it's possible that the computer will go down before you finish your "logic"..

Checkout this handy script to see how it's done..
 
OP
Y
Joined
Sep 8, 2017
Messages
5
Reaction score
0
Points
1
Trying to find a way to run a shutdown hook on mac from a command-line background process.

Tried using NSWorkspaceWillPowerOffNotification, which was never called. Also tried kIOMessageSystemWillPowerOff event and that did not run either.

Anyone has any idea?

OK, I see what you are trying..you said background process, if that is a shell script, one possibility is to trap the script being killed and then run a function where you logic resides. However, this is not a deterministic way of handling the case, i.e., the system is killing all running scripts and expects that to happen quickly and you can't too long or delay the shutdown, so it's possible that the computer will go down before you finish your "logic"..

Checkout this handy script to see how it's done..

Yeah I tried that..
The only down fall to this is the need to run sudo..
Is there are a way to run this on command line?
launchctl load will do the trick?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
You can modify the script to have the startup() function just to the wait since you want the script to hang around. With that, you only need the sudo for Launchctl once to add it to to the list of scripts/functions/applications that will be loaded for each user automatically. When the script is killed, it will not need nor get sudo access, you will want to use a log file location that is accessible to that user.
 
OP
Y
Joined
Sep 8, 2017
Messages
5
Reaction score
0
Points
1
You can modify the script to have the startup() function just to the wait since you want the script to hang around. With that, you only need the sudo for Launchctl once to add it to to the list of scripts/functions/applications that will be loaded for each user automatically. When the script is killed, it will not need nor get sudo access, you will want to use a log file location that is accessible to that user.

But I will still to run one sudo :)

Not sure I also understood what to do with the startup(), according to what you said..
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Yes, you will need Sudo to add to the list of things to run on user login..not sure there's a way around that..
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top