workaround for no auto-sleep problem

Version 1
Last Modified: Feb 22, 2011 5:58 PM
My Mac pro doesn't auto-sleep. I've done an extensive search for possible solutions on apple support and elsewhere but nothing seems to cure it.

I made a little sleep program of my own which I'd like to recommend to anyone with the same problem. It's a very simple shell script that checks system idle time and if it's bigger than the sleep timer you set in system preferences it puts your computer to sleep. It will work so long as you can manually put your computer to sleep from the apple menu.
You'll also need to create a launch daemon to periodically run the script.

here are the steps to do it.

1. copy and paste the following into TextEdit.

#!/bin/bash
*idl=$"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`"*
*sleeptimer=$"`defaults read /Library/Preferences/SystemConfiguration/com.apple.PowerManagement "Custom Profile" | grep "System Sleep Timer" | awk '{print $5}' | sed -e 's/;//g'`"*
st=`expr $sleeptimer \* 60`
*if \[ $idl -gt $st \]; then*
*shutdown -s now*
fi


\\


2. Format it as plain text (shiftcommandT in TextEdit) and save it as sleep.sh (don't use the .txt extension) at the top level of your hard drive. (You can save it elsewhere of course but make sure it's accessible to every user).

3. make it unix executable by entering the following command in terminal.

*chmod 755 /sleep.sh*

4. Download and install [Lingon|http://lingon.sourceforge.net> .

5. Open Lingon and create a new Users Daemon

Call it,say, com.sleep.launchd (the name is not important)

On line 2. enter /sleep.sh

and in section 3 set it to run every 30 seconds (that can be varied).

Click save (you'll have to enter your admin password).

6. Restart.

That's it. Once this is done you can forget about it. The sleep settings are still controlled from system preferences. The sleep agent will work with every user and even over the login window.

To uninstall it, open , select the launch daemon you made and uncheck the box "Enabled" in the upper right corner. save and restart.

Message was edited by: V.K.
correction:

To uninstall it, open Lingon, select the launch daemon you made and uncheck the box "Enabled" in the upper right corner. save and restart.
this tip is ready for consideration.
Hi V.K.

Thanks for the tip. I may have found a bug.

I just tested the tip on my iMac with a run interval of 60 seconds (Step 5).
After restart my iMac was going to sleep every 60 seconds, idle or not.
Seems my Energy Saver was set to never sleep.
I changed it to sleep after 1 hour 30 minutes and the issue went away.
Set it back to never and the issue returned.
Can we have a trap for when the Energy Saver is set to never sleep?
It was quit a surprise to see it sleep before it finished launching all my login items.

Thanks,
Nubz N.