Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.6: Fix sleep problems with SleepWatcher' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Fix sleep problems with SleepWatcher
Authored by: Andrew J Freyer on Dec 02, '10 10:34:48AM
Generally, I don't think its a good idea to recommend launchd modifications/additions without understanding exactly what they're doing. The reason is that if they cause issues they are difficult to debug for the average user. Especially in circumstances like this where a slight error could cause the computer to continually sleep or shut down.

That being said, although I have not tested this script either, this seems like a valid method of forcing an idle-time based sleep if other methods fail.

This could benefit from some explanation as to what the launchd file actually does. First, the option "KeepAlive" will perform the "ProgramArguments" whenever the process sleepwatcher is not running. Similarly "RunAtLoad" will perform the "ProgramArguments" at login.

As for the actual program arguments, they are a bit more complex:

 
<string>/usr/local/sbin/sleepwatcher</string>
<string>-V</string>
<string>-S /usr/local/sbin/sleepwatcher -d -t9000 -i "/usr/local/sbin/sleepwatcher -n" -p /tmp/sleepwatcherid</string>
<string>-W kill `cat /tmp/sleepwatcherid`</string>

The first line calls an instance of sleepwatcher with three arguments: -V (for verbose logging), -S (to run a command on display sleep), -W (to run a command on display wake)

The -S command will run the command

/usr/local/sbin/sleepwatcher -d -t9000 -i "/usr/local/sbin/sleepwatcher -n" -p /tmp/sleepwatcherid
every time the display sleeps. This command launches ANOTHER process of sleepwatcher which is a daemon (-d) that waits for 900 seconds (-t9000) of idle activity (-i) before it performs the command
/usr/local/sbin/sleepwatcher -n
while writing ITS OWN (read: second instance of sleepwatcher) process identifier PID to the file /tmp/sleepwatcherid.

Now, the idle command

/usr/local/sbin/sleepwatcher -n
launches a THIRD instance of sleepwatcher with the single argument -n that tells the THIRD instance of sleepwatcher to cause the computer to sleep and immediately quits the THIRD INSTANCE.

Lastly, the -W command will kill the SECOND instance of sleepwatcher, leaving the first instance running.

Finally, I might note that this is very easily removed from launchd with the same effect. By adding a "-d" tag to the FIRST instance (with the four arguments), and calling the entire command from AppleScript, the result will be the same (so long as the script is saved as an application and is added to the login apps).

Always make sure that you understand what a script is going to.

---
Manage Mac OSX settings depending on your location - AirPort Location for Mac



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: zyuvi00 on Dec 03, '10 04:16:36PM

So I decided to follow through on Andrew's suggestion and create a user-level application (via Automator in my case since I'm not too script savvy). If anyone is interested, here is what I did:

1. Create an Automator "Application"

2. Add a "Run Shell Script" action to the workflow.

3. Paste the following text into the shell script window:
/usr/local/sbin/sleepwatcher -d -S '/usr/local/sbin/sleepwatcher -d -t6000 -i "/usr/local/sbin/sleepwatcher -n" -p /tmp/sleepwatcherid' -W 'kill -9 `cat /tmp/sleepwatcherid`'

4. Replace "6000" with however long you want it to wait. (Value is in 0.1 seconds so 6000 = 10 minutes.)

5. Save the application.

6. Go to "Systems Preferences > Accounts" and add this application to your Login items

Since I'm new to the Mac I was nervous about this doing things like continually launching more and more processes without killing them properly or something like that, so I tested it by setting the sleep time to 10 seconds (-t100) and running it in a bunch of scenarios while watching the Activity Monitor... Seems to work for me.

Thanks for all the pointers in this discussion thread -- helped me work around a frustrating problem with my system crashing every time the OS tried to put my system (with a newly installed SSD) to sleep via the Energy Saver Prefs Pane...



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: agitatedString on Oct 30, '11 12:59:12AM

Hackintoshes? Anyway, getting sleepwatcher to work for me was as simple as putting the following into my ~/.bash_profile file:

result=$(lsof | grep -i sleepwatcher)
if [ -z "$result" ]; then
/opt/local/sbin/sleepwatcher --daemon --sleep /Users/pcs/bin/sleepscript.sh --wakeup /Users/pcs/bin/wakescript.sh
fi



[ Reply to This | # ]