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

canistel

macrumors newbie
Original poster
Apr 24, 2013
7
0
Hi, I have a mac mini setup running in headless mode (no screen attached). I need to power off the mac when the power button is pressed, and there should be no confirmation dialog appearing.... as soon as button is pressed, begin shutdown procedure. It must not be a hibernate or sleep / suspend mode either, but needs to be a complete "halt" / power down. (It's a special project, don't want to go into the reasons why, but suffice it to say that those are requirements that cannot be changed)

Is there any way to do this? It is so easy on windows and linux I have to assume I'm missing the obvious somewhere here, but for the life of me I can't figure it out on osx...

Thanks!
 

benwiggy

macrumors 68020
Jun 15, 2012
2,381
193
Press the power/on off button few a few seconds(or until it shuts down).
Errr, no. That's a bad idea. Holding down the power button forces an immediate power off, without OS X doing any "housekeeping" that normally occurs in the shutdown process. You can cause disk corruption and it's not great for the hardware to do this repeatedly.

Is there a keyboard attached? It's easy to shutdown the computer with a keyboard shortcut (though you might run in trouble without a monitor if something cancels the shutdown for some reason.)

I don't think this is possible without a keyboard, though. A quick Google doesn't reveal much -- I presume you've done a thorough search? Most headless computers are rarely powered off.

Alternatively, you can schedule shutdowns and startups in Energy Saver preferences. Might that be good enough?
 

canistel

macrumors newbie
Original poster
Apr 24, 2013
7
0
Thanks for the replies...

The computer is less of a "headless server" and more of an appliance. It has no keyboard, mouse, or screen. It needs to do a full power up and full shutdown with a press of the button (it's the only button attached directly to the machine as far as that goes).

I agree that hanging onto the powerbutton does the job, but it's very dangerous as already pointed out. Scheduling isn't going to work either, there is no rhyme or rhythm to when this thing is used.

I can build and execute a bash script which does what I need to (basically "shutdown -h now"), but how would I make that script execute when power button is pressed? Can I catch the power button "event" and launch a script what that happens? Maybe via automator or apple script??

Any other ideas? Can't believe this is so difficult considering how easy it is on other systems :(
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
Looks like you aren't going to be able to do it. The best information I see is from the 'pmset' shell command. The machine will always sleep when you hit the power button. The best you can do is have it hibernate and turn off. But when you start, it will restore from disk image. There's really no way around that.

The only other option I can think of is to attach some kind of USB thing with a switch and write a program to list for events from it. Then that program could shut the machine down.

Or install Linux on the Mac mini and have it do whatever you want.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,739
8,410
A sea of green
There is no "event" for the power button.

1. You could enable remote AppleScripting, then run a script from another Mac that tells the headless one to shutdown.

2. You could enable remote access via SSH, then connect using SSH from another computer, and issue the 'shutdown' command. Google search terms: mac os enable ssh

3. You could use a wireless keyboard and/or mouse.


On what other systems do you think this is so much easier? I'm curious, because all recent versions of Windows prefers an orderly shutdown, and so do all the server OSes I can think of, including all Linux-based ones. It's been quite a while since one could simply turn off power and not risk damage to file-systems.
 

canistel

macrumors newbie
Original poster
Apr 24, 2013
7
0
There is no "event" for the power button.

1. You could enable remote AppleScripting, then run a script from another Mac that tells the headless one to shutdown.

2. You could enable remote access via SSH, then connect using SSH from another computer, and issue the 'shutdown' command. Google search terms: mac os enable ssh

3. You could use a wireless keyboard and/or mouse.


On what other systems do you think this is so much easier? I'm curious, because all recent versions of Windows prefers an orderly shutdown, and so do all the server OSes I can think of, including all Linux-based ones. It's been quite a while since one could simply turn off power and not risk damage to file-systems.

This is an application project, I've already extended the power switch several feet, there is no other computer available to send the shutdown via ssh or whatever. It's a 100% closed system / application. No keyboard or mouse or monitor...

I'm not talking about an immediate power off risking damage to the file system; I'm talking about an orderly shutdown of os x, triggered by the power button event with no confirmation dialog popping up (because I have no way of closing the stupid thing). On windows and linux you can set it up so that the power button immediately begins to shutdown (safely) the computer, no more interaction with the machine is required and it will eventually power off. What I'm hearing here is that apparently osx cannot do that, you can only suspend or hibernate the system... this application requires the full shutdown, no suspend, no hibernate.

Thinking about wiping this thing and installing windows 7....
 

benwiggy

macrumors 68020
Jun 15, 2012
2,381
193
Thinking about wiping this thing and installing windows 7....
Well, you should install whatever OS is optimal for the highly specialised task you require.

I am surprised that other OSes can do this, because as pointed out, there is no software event generated by the power switch. So it's quite surprising that Windows and Linux know how to deal with a Mac Mini power switch being pressed.
 

canistel

macrumors newbie
Original poster
Apr 24, 2013
7
0
I am surprised that other OSes can do this, because as pointed out, there is no software event generated by the power switch. So it's quite surprising that Windows and Linux know how to deal with a Mac Mini power switch being pressed.

Of course there is an event... Not trying to be argumentative, but how do you think OS X can show the confirmation dialog? The OS (whether linux / windows / osx) knows when the powerbutton is switched. On my windows 7 machine (built from off the shelf hardware but assembled by myself) I press the power button and windows 7 starts turning off immediately, no messages, no questions, no fuss... 10 seconds after pressing the button the machine is totally powered off, safely.

What I was asking for with the "capture the event" question was whether I could hook my own application into the OS so that it (my app) gets notified when the power button is pressed. If that were possible, then I could execute "shutdown -h now" from my application and bypass the whole mess...
 

Wondercow

macrumors 6502a
Aug 27, 2008
559
365
Toronto, Canada
Of course there is an event... Not trying to be argumentative, but how do you think OS X can show the confirmation dialog? The OS (whether linux / windows / osx) knows when the powerbutton is switched. On my windows 7 machine (built from off the shelf hardware but assembled by myself) I press the power button and windows 7 starts turning off immediately, no messages, no questions, no fuss... 10 seconds after pressing the button the machine is totally powered off, safely.

What I was asking for with the "capture the event" question was whether I could hook my own application into the OS so that it (my app) gets notified when the power button is pressed. If that were possible, then I could execute "shutdown -h now" from my application and bypass the whole mess...

Try this:

Code:
on idle
	set i to 0
	
	tell application "System Events"
		
		-- Get a count of all the windows belonging to the process
		set numberOfWindows to count windows of process "loginwindow"
		
		-- Check each window for a "Shut Down" button....
		repeat numberOfWindows times
			set i to i + 1
			
			-- ....and shut down if found
			if exists button "Shut Down" of window i of process "loginwindow" then
				do shell script "shutdown -h now"
			end if
		end repeat
	end tell
	
	-- How often the script checks for the window (in seconds). Change it as you see fit.
	return 0.5
end idle
This should be ready to go; copy to Applescript, save it as an application with the "Stay open" option selected, add it to your Login Items.
 

canistel

macrumors newbie
Original poster
Apr 24, 2013
7
0
Try this:

Code:
on idle
	set i to 0
	
	tell application "System Events"
		
		-- Get a count of all the windows belonging to the process
		set numberOfWindows to count windows of process "loginwindow"
		
		-- Check each window for a "Shut Down" button....
		repeat numberOfWindows times
			set i to i + 1
			
			-- ....and shut down if found
			if exists button "Shut Down" of window i of process "loginwindow" then
				do shell script "shutdown -h now"
			end if
		end repeat
	end tell
	
	-- How often the script checks for the window (in seconds). Change it as you see fit.
	return 0.5
end idle
This should be ready to go; copy to Applescript, save it as an application with the "Stay open" option selected, add it to your Login Items.

Very slick, thanks a ton!

One minor nit (and only for completeness in case anyone else wants to do this too) is that the first times I tried this AppleScript gave me some sort of "accesibility error"; forget the exact message, but it was an accessibility error message (or maybe "assisitive"?). After googling it I found the fix was just to go into System Preferences, Accessibility, then select the "Enable access for assistive devices".

This is my first run in with apple script, very intestesting tech... while googling other pieces of the code I discovered that you can also send key presses or key strokes... so we could probably modify the script above so that instead of executing the shell script it simply "activates" that window and then presses the "Enter" key? Since the "Shut Down" button is the highlighted button in that window, focusing / activating the window and then hitting enter will begin the shutdown.

My reason for suggesting this change is that the "shutdown -h now" doesn't work straight-out-of-the-box... I actually had to modify that to "sudo shutdown -h now" instead, and then configure sudo to allow my user account to execute with root priviledges without entering a password...

But thanks very much for the script and getting me 95% there, much appreciated!
 

Wondercow

macrumors 6502a
Aug 27, 2008
559
365
Toronto, Canada
Very slick, thanks a ton!

One minor nit (and only for completeness in case anyone else wants to do this too) is that the first times I tried this AppleScript gave me some sort of "accesibility error"; forget the exact message, but it was an accessibility error message (or maybe "assisitive"?). After googling it I found the fix was just to go into System Preferences, Accessibility, then select the "Enable access for assistive devices".

This is my first run in with apple script, very intestesting tech... while googling other pieces of the code I discovered that you can also send key presses or key strokes... so we could probably modify the script above so that instead of executing the shell script it simply "activates" that window and then presses the "Enter" key? Since the "Shut Down" button is the highlighted button in that window, focusing / activating the window and then hitting enter will begin the shutdown.

My reason for suggesting this change is that the "shutdown -h now" doesn't work straight-out-of-the-box... I actually had to modify that to "sudo shutdown -h now" instead, and then configure sudo to allow my user account to execute with root priviledges without entering a password...

But thanks very much for the script and getting me 95% there, much appreciated!
Yeah, I forgot to mention that you'd need to activate access for assistive devices :eek:. To change to a "normal" shut down just replace this line:
Code:
do shell script "shutdown -h now"
with this:
Code:
click button "Shut Down" of window i of process "loginwindow"
Remember, though, that this be cancelled by any process that asks to save changes to open documents.

And, FWIW, "shutdown" is enabled out-of-the-box and "-h now" just tells the system to do it now, do not wait. Using this method will shut down regardless of unsaved changes, open documents, etc. [I just re-read the end of your post and I see that I misread your statement!]
 
Last edited:

canistel

macrumors newbie
Original poster
Apr 24, 2013
7
0
Ok; so "shutdown -h now" can't be interuptted in the same way that it can be when pressing "Shut Down" button in that window? If so, then I'll just leave my script alone, definitely need to force a controlled shutdown at that point...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.