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

Implement automatic screen locking prior to sleep System
The goal: lock the screen with standard Screen Effects automatically, right before switching the Mac to sleep state. This is a standard security precaution, and I don't know why it's not implemented in Jaguar, but here is a workaround.

There is nice free daemon called sleepwatcher; it can run some scripts on sleep or wake events. Install it and call /System -> Library -> Frameworks ->ScreenSaver.framework ->Resources -> ScreenSaverEngine.app -> Contents -> MacOS -> ScreenSaverEngine from its "sleep" script. Simple, but there is problem. If you already have ScreenSaverEngine running, a second instance will screw both of them (not deadly, but unpleasant). To prevent this, it should be called like this:
#!/bin/bash

SCRS=`ps xcopid,command|grep ScreenSaverEng|cut -b1-5`

if [ -z "$SCRS" ]; then
  # Next line is shown on TWO LINES; enter as one
  # without any added spaces...
  /System/Library/Frameworks/ScreenSaver.framework/Resources/
  ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine &

  sleep 3
fi
That's it. Screen Effects should be configured to lock the screen, of course.

[robg adds: I haven't tested this one, but I took a look at the sleepwatcher package. I think (hopefully someone can confirm) that you make this work by creating the above script and saving it as .sleep in your home directory (remember to make it executable). sleepwatcher then checks for the .sleep file before the system goes to sleep and locks your screen. I also can't confirm that you're then asked for the password on wake -- anyone?]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[11,778 views]  

Implement automatic screen locking prior to sleep | 18 comments | Create New Account
Click here to return to the 'Implement automatic screen locking prior to sleep' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Implement automatic screen locking prior to sleep
Authored by: mattbrink on Jul 17, '03 11:24:42AM
I was in a similar situation but I wanted to be able to manually sleep+lock the machine (usually overnight.) I created this Applescript to do the job. (Not as cool as the solution above but it is a bit simpler.)

tell application "ScreenSaverEngine" to run
delay 5
tell application "Finder"
sleep
end tell

[ Reply to This | # ]

Try SleepTight!
Authored by: CrankyCat on Jul 17, '03 11:37:54AM

I too was using sleepwatcher to lock my screen on sleep, but recently an easier solution has come along: SleepTight

http://www.ragingmenace.com/software/sleeptight/index.html

I still use sleepwatcher to rebuild a ssh tunnel for my email at wakeup though...



[ Reply to This | # ]
Try SleepTight!
Authored by: rsfinn on Jul 17, '03 12:07:31PM

Hey, that's just the functionality I need! Mind sharing your script?



[ Reply to This | # ]
know how to unmount network drives on sleep?
Authored by: jonny5 on Jul 17, '03 11:54:20AM
Great hint - does anyone know of an easy script to unmount network drives that can be executed on sleep by this? I have been looking for something like this to avoid the unpleasant effects of changing networks w/ old drives still mounted....

This hint mentions disktool and diskutil. I suppose you could do something like this in the .sleep script:

for VOL in $(ls /Volumes); do
diskutil eject /Volumes/"$VOL"
done


but, are there any potential issues? what if a finder window was open for one of those volumes? or an app had a file open?

unfortunately I can't test this now... I'm away from my network. thanks, Jon

[ Reply to This | # ]
know how to unmount network drives on sleep?
Authored by: triplef on Jul 18, '03 08:40:49AM

Nice idea! I tried using

for VOL in /Volumes/*; do
        diskutil eject "$VOL"
done

in my .sleep file, which works nice with mounted disk images. However, I cannot get a disk mounted from a Linux box over Netatalk to eject. It simply doesn't work, even when I execute the command manually.

bash-2.05a$ diskutil eject "/Volumes/Space"
Disk Utility Tool       Š2002, Apple Computer, Inc.
Usage:  diskutil [mount(Disk)|unmount(Disk)|eject]
[Mount Point|Disk Identifier|Device Node]
Mount, unmount or eject disks or volumes.  Root access is not required.
Example:  diskutil unmount /Volumes/SomeDisk

Any ideas?



[ Reply to This | # ]
know how to unmount network drives on sleep?
Authored by: jonny5 on Jul 18, '03 09:42:40AM

Now that I'm at home & attempting this w/ my network, it seems that diskutil doesn't work (unmount or eject) for disks shared via SMB either. Anyone know how to unmount these via the command line???



[ Reply to This | # ]
There is way to unmount network volumes
Authored by: atverd on Jul 18, '03 01:12:51PM

umount /Volumes/name
will do, so you can put it as second similar loop in your script after first pass with diskutil. But after all you'll need to run
disktool -r
to refresh internal table of mounts used by Finder and Co (to remove icons from desktop).



[ Reply to This | # ]
This will cause the flickering screensaver of death!
Authored by: Basilisk on Jul 17, '03 12:57:15PM

I've posted info on this before. Here it is again (editted slightly for clarity)...

This technique is a bad idea. It will cause the flickering screensaver issue (double-launched saver).

Yes, I'm aware that this method is popular, and is even covered in many hints on this site, but it is wrong, and doesn't do what people think. Launching the screensaver engine directly is not the same method used by the system itself. Try the following test (be careful, this leads to a problem documented below):

1. Set you system screensaver start time to 5 minutes.
2. Launch ScreenSaverEngine.app directly (same way as the suggested script)
3. Wait 5 minutes.

After the 5 minutes have passed the windowmanager will start the screensaver a second time, leading to flickering screensaver of doom (both savers fight to be front and recieve keystrokes during password entry). With some patience you can usually get one to take a password and eventually quit, or just force restart the machine.

Properly launching the screensaver requires the use of an undocumented API in ScreenSaver.framework, which uses the windowmanager to launch the screensaver. This works correctly, and the screensaver won't double launch (no flicker of doom) and doesn't conflict with the desktop screensaver.

How do I know all this? Becuase I had a similar problem and had to figure out how Apple's keychain menu extra does this properly (never double-launched, no conflcit with desktop background). I've since written an app to do this properly, get SleepTight:

http://www.ragingmenace.com/software/sleeptight/index.html

The current 1.0 version has a pref pane bug causing the pref to crash on some systems (this does not affect the agent which actually launches the saver). I'll have a 1.0.1 release available shortly.

Bas



[ Reply to This | # ]
This will cause the flickering screensaver of death!
Authored by: atverd on Jul 17, '03 04:25:23PM

Confirmed, though I never had this problem in real life. I'd sugest SleepTight solution instead of this script as more clean. But actually that flickering "screen of death" is not deadly at all. If you ssh to machine over network and kill all running instances of ScreenSaver everything will be just fine.

And in any case - sleepwatcher still very usefull program.

Thank you, Bas for another great program after my favorite MenuMeters :)



[ Reply to This | # ]
I don't get it
Authored by: benfsmith on Jul 17, '03 02:08:02PM

Why not just use Apple's Keychain menu extra to do this?



[ Reply to This | # ]
I don't get it
Authored by: kevinv on Jul 17, '03 09:17:51PM

The keychain menu bar item requires a person to turn on the lock. If a computer goes to sleep before the screen saver kicks in, when you wake it up it won't ask for a password. This method automatically locks the computer when the sleep mode is activated.



[ Reply to This | # ]
I don't get it
Authored by: tbmaddux on Jul 18, '03 09:10:13AM
If a computer goes to sleep before the screen saver kicks in, when you wake it up it won't ask for a password.
So the solution is to use the Screen Effects preference pane. In the Activation tab there is a preference for "Password to use when waking the screen effect" -- this will take care of the computer sleeping itself if you have not locked your own screen. Just be sure to set your screen effect to activate before the computer sleep time. If you want to manually lock, you can still use the Keychain menu extra.

[ Reply to This | # ]
I don't get it
Authored by: rusto on Jul 18, '03 09:58:50AM

That will be fine if you want your computer to sleep at some point after you activate the screen saver but the point of this hint is to enable you to activate the password protected screen saver then go directly to sleep WITHOUT DELAY.



[ Reply to This | # ]
Implement automatic screen locking prior to sleep
Authored by: omnivector on Jul 17, '03 03:04:43PM

well, i'm sure you all will be pleased to know panther locks the screen after awaking from sleep by default (i didn't like the feature so i turned it off).

---
- Tristan



[ Reply to This | # ]
Implement automatic screen locking prior to sleep
Authored by: Basilisk on Jul 17, '03 09:00:21PM

I used to get bitten by the flicker all the time. I used a program called SleepLock (I think it can still be found on MacUpdate) that caused it.

As for the "Of Death" name, you are, of course, correct. SSH and kill is an option for most OSX Hints readers I'm sure, but not for my girlfriend's Pismo, which is where I coined the term.

Besides, it has to have a cool name, I can't just call it the "Flickering ScreenSaver of Great Annoyance" :-)

SleepWatcher is excelllent. I use it myself for other things, and in fact uses the SleepWatcher source to learn how to write SleepTight.

Thanks for the kind words on MenuMeters, enjoy,

Bas



[ Reply to This | # ]
Implement automatic screen locking prior to sleep
Authored by: Basilisk on Jul 17, '03 09:02:10PM

Oops. Meant to reply to the thread above. Sorry.



[ Reply to This | # ]
Implement automatic screen locking prior to sleep
Authored by: Anonymous on Aug 13, '03 06:38:37AM

For some reason, when I set my screen saver to activate after a predetermined period of time, the screen saver does not activate. I have tried everything from running Jaguar Cache Cleaner and Cocktail, as well as disabling my login items. Sometimes the screen saver works and sometimes not. What gives?

What exactly controls when the Screen Saver activates? Is there a file somewhere that I can tweak? Or is there a file or files that need to be trashed. I trashed the Screen saver pref files to no avail. I'm at the end of my rope on this one.

What does work is the ability to manually activate the screen saver by going to a sleep corner. My problem is that if I leave my Mac for a few minutes, I would expect when I come back, to have the screen saver on, without me manually having to activate it. Look forward to hearing back from someone soon. Thanks for all of your help!!!



[ Reply to This | # ]
Implement automatic screen locking prior to sleep
Authored by: ptman on Oct 05, '06 05:08:48AM
Instead of starting the screensaver: /System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend

[ Reply to This | # ]