Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Mac Basics and Help

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 24, 2013, 08:49 AM   #1
canistel
macrumors newbie
 
Join Date: Apr 2013
power off computer via power switch with no confirmation dialog

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!
canistel is offline   0 Reply With Quote
Old Apr 24, 2013, 09:25 AM   #2
Dave Braine
macrumors 68020
 
Join Date: Mar 2008
Location: Warrington, UK
Press the power/on off button few a few seconds(or until it shuts down).
__________________
PPC Mini, 10.4.11, Intel Mini, 10.6.8, White MacBook 10.9.2
iPhone 3GS, using o2 PAYG
Dave Braine is offline   0 Reply With Quote
Old Apr 24, 2013, 10:56 AM   #3
benwiggy
macrumors 68020
 
Join Date: Jun 2012
Quote:
Originally Posted by Dave Braine View Post
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?
__________________
2012 MacMini, 2.6GHz i7, 16Gb RAM, Fusion Drive | 2012 MacBook Pro, 16Gb RAM, 480 Gb SSD |
2009 MacBook | 2006 iMac | 2003 G3 iBook | Beige G3 | PowerMac 7600 | Mac IIsi |
benwiggy is offline   0 Reply With Quote
Old Apr 24, 2013, 01:50 PM   #4
canistel
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
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
canistel is offline   0 Reply With Quote
Old Apr 24, 2013, 03:30 PM   #5
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: 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.
mfram is offline   0 Reply With Quote
Old Apr 24, 2013, 06:23 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
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.
chown33 is offline   0 Reply With Quote
Old Apr 25, 2013, 07:26 AM   #7
canistel
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
Quote:
Originally Posted by chown33 View Post
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....
canistel is offline   0 Reply With Quote
Old Apr 25, 2013, 07:44 AM   #8
benwiggy
macrumors 68020
 
Join Date: Jun 2012
Quote:
Originally Posted by canistel View Post
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.
__________________
2012 MacMini, 2.6GHz i7, 16Gb RAM, Fusion Drive | 2012 MacBook Pro, 16Gb RAM, 480 Gb SSD |
2009 MacBook | 2006 iMac | 2003 G3 iBook | Beige G3 | PowerMac 7600 | Mac IIsi |
benwiggy is offline   0 Reply With Quote
Old Apr 25, 2013, 08:37 AM   #9
canistel
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
Quote:
Originally Posted by benwiggy View Post
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...
canistel is offline   0 Reply With Quote
Old Apr 25, 2013, 09:32 AM   #10
Wondercow
macrumors regular
 
Join Date: Aug 2008
Location: Toronto, Canada
Quote:
Originally Posted by canistel View Post
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.
Wondercow is offline   0 Reply With Quote
Old Apr 25, 2013, 02:19 PM   #11
canistel
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
Quote:
Originally Posted by Wondercow View Post
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!
canistel is offline   0 Reply With Quote
Old Apr 25, 2013, 03:22 PM   #12
Wondercow
macrumors regular
 
Join Date: Aug 2008
Location: Toronto, Canada
Quote:
Originally Posted by canistel View Post
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 . 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 by Wondercow; Apr 25, 2013 at 03:36 PM.
Wondercow is offline   0 Reply With Quote
Old Apr 25, 2013, 03:28 PM   #13
canistel
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
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...
canistel is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Mac Basics and Help

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Anyone switch to solar power yet? iBlazed Politics, Religion, Social Issues 23 Mar 26, 2014 10:46 AM
No power sent to HDMI switch? iamsen47 Mac mini 6 Feb 25, 2014 10:35 PM
KVM switch or Software for Power PC G5, 10.4.11? tanquiano PowerPC Macs 1 Sep 4, 2013 04:44 PM
Power Mac G5 Power Switch iAdvisorUK PowerPC Macs 3 Apr 8, 2013 02:33 PM
It's got to be close to time to switch if you need power lloyd709 Mac Pro 13 Dec 14, 2012 05:50 PM

Forum Jump

All times are GMT -5. The time now is 03:47 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC