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

A helper script to enter SafeSleep on demand System 10.4
If you're using 10.4.3, you may have already tried out enabling Safe Sleep on your computer using the tips from Andrew Escobar's blog. I found it works great on my Powerbook G4 867, but I didn't want to have to keep using the Terminal to switch it from hibernatemode 3 (computer uses regular sleep, only Safe Sleeps in extreme situations) to hibernatemode 1 (goes straight into Safe Sleep).

So I wrote an AppleScript application that flips the machine to mode 1, sleeps the machine, then turns it back to mode 3 upon awaking, so that I can use Safe Sleep on demand.

The code is as follows:

do shell script "/usr/bin/sudo -k;/usr/bin/sudo /usr/bin/pmset -a hibernatemode 1; /usr/bin/sudo -k" password  
 "_password_" with administrator privileges
ignoring application responses
  tell application "Finder" to sleep
  do shell script "(/bin/sleep 15 && /usr/bin/sudo -k && 
   /usr/bin/sudo /usr/bin/pmset -a hibernatemode 3 
   && /usr/bin/sudo -k) &> /dev/null &" password 
   "_password_" with administrator privileges
end ignoring

Save this as a Run Only application with no Startup Screen, and you've got a great Dock item to use when swapping a battery with no power cord (or whatever).

As an added bonus, save it as an Application Bundle type (in the Save dialog), and then edit the Info.plist file you'll find inside the application's package, and add these lines:
<key>LSUIElement</key>
<string>1</string>
Now you have a handy faceless application, suitable for use with your favorite keystroke launcher.

Things to adjust:
  • Need to put your administrator password inside -- replace _password_ with your admin password. If you want the program to ask you each time, just remove the password "_Password_" argument altogether.
  • You may want to play with the sleep value of 15 seconds in the second shell line. This may be too long for your tastes.
[robg adds: I haven't tested this one. A very recent hint on Safe Sleep links to Andrew's instructions as well...]
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (5 votes cast)
 
[35,519 views]  

A helper script to enter SafeSleep on demand | 14 comments | Create New Account
Click here to return to the 'A helper script to enter SafeSleep on demand' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A helper script to enter SafeSleep on demand
Authored by: Anonymous on Nov 21, '05 08:02:44AM
A good hint. If you're using Secure Virtual Memory, the script needs a few changes:

do shell script "/usr/bin/sudo -k;/usr/bin/sudo /usr/bin/pmset -a hibernatemode 7; /usr/bin/sudo -k" password "_password_" with administrator privileges
ignoring application responses
	tell application "Finder" to sleep
	do shell script "(/bin/sleep 15 && /usr/bin/sudo -k && /usr/bin/sudo /usr/bin/pmset -a hibernatemode 5 && /usr/bin/sudo -k) &> /dev/null &" password "_password_" with administrator privileges
end ignoring
The difference is that '/usr/bin/pmset -a hibernatemode' in line 1 is set to 7, and /usr/bin/pmset -a hibernatemode' in line 4 is set to 5.

[robg adds: I added the div box to narrow the display down substantially. Also note the following comment, which indicates that the 7 and 5 need to be reversed if you use this script -- I did not, however, want to change the code, as I haven't tested it myself.]

[ Reply to This | # ]
It should be the 5 first time, THEN the 7! (NT)
Authored by: gabrielradic on Nov 22, '05 02:25:06PM

It should be the 5 first time, THEN the 7! (NT)

The above script got it the other way around, but it works well otherwise.



[ Reply to This | # ]
Works great ! half of the times
Authored by: Ludowan on Sep 20, '06 08:37:40PM

Hello!
the script works great on my PB G4 1.5GHz about half the times. The other half leaves the computer stuck during the 'wake up' process (progress bar does not 'progress' anymore at some varying stage). Sometimes it goes all through the wake up process, and when the screen comes back to its original state, gets stuck here (no mouse, no keyboard, no clock, nothing).
What is happening?

Also, what is the use of the "/bin/sleep 15" command? What does it do if I change it to 30, or to 0?
Thanks!



[ Reply to This | # ]
Sudo?
Authored by: guardian34 on Nov 21, '05 08:22:46AM

Do you really need to use sudo?

TN2065: do shell script

Note: Using sudo(8) with with administrator privileges is generally unnecessary and creates security holes; simply remove the "sudo".


[ Reply to This | # ]
A helper script to enter SafeSleep on demand
Authored by: mark hunte on Nov 21, '05 09:09:51AM
I tried
pmset force -a hibernatemode 1
or
pmset force -a hibernatemode 3
which works with out the password. It works as virtual pref which is held in the memory. If you reboot or open the energy saver or the other pmset the real pref will be invoked.

---
mh

[ Reply to This | # ]

A helper script to enter SafeSleep on demand
Authored by: luche on Mar 24, '06 01:18:55PM

thought i'd mention... so i'm not sure how many of you have anything for the expresscard slot yet... but i got a sony vaio with a sd/mmc/xd expresscard... which does work wonderfully in my macbook... but if it's plugged in, (ejected in the system or not...) i can't make the computer go to sleep... it'll blank the screen and 10 sec. later - it's running again... removed the card and it sleeps fine... tested it a number of times...

mind you, i wasn't playing with the script posted here, but rather with the pmset -a hibernatemode 3 command...

worth mentioning as well... with hibernatemode 1... (suspend to disk upon sleep) it'll make the disk image just fine and hibernate until i choose to wake it... i'm thinking this is a bug apple needs to take care of... i'll be talking to them next.



[ Reply to This | # ]
Simplified shell script
Authored by: TrumpetPower! on Nov 21, '05 12:27:23PM

For some time I’ve had a simple shell script called “zzz” that was a one-line osascript that just put the computer to sleep. Those of you familiar with OpenBSD (and probably others) will recognize why.

The fact that OS X is case-insensistive-but-preserving, combined with friendly’s hint, led me to update that one-liner. Save this somewhere in your $PATH and you'll be able to use zzz to sleep normally, and ZZZ (that’s all upper-case) to do a Safe Sleep.


#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin;

if [ `basename ${0}` == "ZZZ" ]; then

    OriginalHibernateMode=`pmset -g | awk '/hibernatemode/ {print $2}'`;
    sudo -v;
    if sysctl vm.swapusage | grep -q 'encrypted'; then
        sudo sh -c "pmset -a hibernatemode 5; sleep 16; pmset -a hibernatemode ${OriginalHibernateMode}" &
    else
        sudo sh -c "pmset -a hibernatemode 1; sleep 16; pmset -a hibernatemode ${OriginalHibernateMode}" &
    fi
fi

osascript -e 'tell application "Finder" to sleep';

Now, a perfect accompaniment to this would be a hack that used the sleep / lid LED light as a power indicator. I’ve always been convinced that that light should be on steady and bright when the power is on; on steady but dim when running off battery; do the cycle / glow thing when sleeping; and be off when the computer is off. As it is now, you can’t tell the differece between a computer with the backlight off and one with the whole thing powered down, unless you try to wake it….

Cheers,

b&



[ Reply to This | # ]
Simplified shell script
Authored by: pecosbill on Sep 27, '06 02:04:33AM
This is great. Combined with this hint and the force hint above, I made a plain text file called Hibernate Now.command and put it on the Desktop. I change the permissions of that file so it would execute:
chmod u+x ~/Desktop/Hibernate\ Now.command
and put the following in it (I don't run secure VM):

#/bin/sh
# For added security:
PATH=/bin:/sbin:/usr/bin:/usr/sbin;

/bin/sh -c "pmset force -a hibernatemode 1; sleep 16; pmset force -a hibernatemode 3" &
/usr/bin/osascript -e 'tell application "Finder" to sleep';
Using the script above, you could set it up to work with either VM mode. The great part is it requires no password nor a set uid bit which makes me nervous when that UID is root.

---
Pecos Bill

[ Reply to This | # ]

A helper script to enter SafeSleep on demand
Authored by: digitol on Nov 21, '05 01:38:24PM

Ok basically similar to this post, just for lazy ppl, the suspendnow app may be a better solution.!!



[ Reply to This | # ]
A helper script to enter SafeSleep on demand
Authored by: digitol on Nov 21, '05 01:35:38PM

Minions, please! :) hehehhehe, how bout you try this: http://www.versiontracker.com/dyn/moreinfo/macosx/28440

suspend now, it a bunch of applescripts and shell scripts.. i have modified it for my best interests. which makes it cool. also with this you can actually safe sleep without having to pull the power on your mac!



[ Reply to This | # ]
Broke my PB 12" !!!
Authored by: gabrielradic on Nov 22, '05 03:54:15AM

The script failed on my PowerBook 12", pre-latest revision.

The Mac went to deep sleep, but when it woke up it kernel-panicked, around the third notch in the progress bar.

A cold reboot didn't help, the computer kept trying to wake-up.

Single-user mode (Ctrl+S) is not active, but you can boot from another device. I used another PowerBook in Firewire mode.

Delete the file /private/etc/vm/sleepimage, it'll fix things.

I hope this helps.



[ Reply to This | # ]
More on this
Authored by: gabrielradic on Nov 22, '05 02:10:27PM

I had the same issue later, since I still have to find a way to disable safe sleep. This time, I've booted the _broken_ powerbook in target mode, and deleted the file using the other Mac.

When I turned on the _broken_ powerbook, it still tried to wake up, and failed at the same point.

Weird.



[ Reply to This | # ]
It was the secured VM, see the first comment (NT)
Authored by: gabrielradic on Nov 22, '05 02:26:43PM

It was the secured VM, see the first comment (NT)



[ Reply to This | # ]
Not quite working on 12" PB
Authored by: rawhead on Nov 24, '05 10:13:05PM

OK, I have the 2003 12" PB/1GHz running Tiger 10.4.3.

The original script to turn SafeSleep mode on worked well. Then I tried this script, and it looked like it did its job. That is, my PB went into hibernation after running the script and it booted up fine.

But then, it seems like the hibernate mode wasn't set back to "3". Ever since then, every time I closed the lid, the PB would hibernate instead of sleeping. I went into Terminal and manually set the hibernate mode back to 3 and looks like things are back to normal.

---
All these moments will be lost in time
Like tears in rain.
Time to die.



[ Reply to This | # ]