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

System sleep depending on login & logout System
The system's sleep function is a great feature; it works stably and saves a lot of time rebooting the system. The problem is, when sleep is enabled, you have the risk that the system goes to sleep when it shouldn't, e.g. during a big download or backup. So the best solution would be for the sleep function to be deactivated when a user is logged in, and activated when nobody is logged in.

So I wrote two small shell scripts, called by the login and logout hooks. Because OS X is a multi user OS, you have to consider that more then one user can login and logout. Therefore, the sleep timer must be activated only when the last user loged out of the system.

Here is the login script login.sh:
#!/bin/sh
#
# create login lock
echo $1 > /tmp/login_$1.LOCK
# deactivate system sleep timer
/usr/bin/pmset -a sleep 0
And the logout script logout.sh:
#!/bin/sh
#
# delete own login lock
rm /tmp/login_$1.LOCK
# test other user is logged in
if [ ! -s /tmp/login_*.LOCK ]
then
  # no other login lock found, activate system sleep timer
  /usr/bin/pmset -a sleep 15
fi
The login script is setting a LOCK file, created with the user name, then it deactivates the sleep timer. The logout script is deleting the user's own LOCK file. Then it tests for other user LOCK files, and when no LOCK files are found, then it activates the sleep timer for 15 minutes. To get the two scripts executed when login/logout occurs, activate them as follow:
sudo defaults write com.apple.loginwindow LoginHook /path/to/script/login.sh
sudo defaults write com.apple.loginwindow LogoutHook /path/to/script/logout.sh
Two simple scripts let you implement a system sleep management.

[robg adds: I haven't tested this one; make sure that the scripts are executable (chmod +x script_name)...]
    •    
  • Currently 2.25 / 5
  You rated: 2 / 5 (4 votes cast)
 
[10,268 views]  

System sleep depending on login & logout | 5 comments | Create New Account
Click here to return to the 'System sleep depending on login & logout' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
System sleep depending on login & logout
Authored by: thrig on Jan 04, '05 11:35:29AM

The following code must not be used where untrusted users have local access as it is a security risk.

echo $1 > /tmp/login_$1.LOCK

A malicious local attacker can simply create symbolic links in advance, then wait for the appropriate user to login:

$ ln -s /mach.sym /tmp/login_john.LOCK

Or automate the process for all users via scripting:

#!/bin/sh

FILE_TO_TRASH=/mach.sym

# populate links from NetInfo database
nidump passwd / | awk -F: '{print $1}' | while read user; do
ln -s $FILE_TO_TRASH /tmp/login_$user.LOCK
done

To avoid this class of security problem, do not use shared /tmp directories if at all possible. Alternatives include creating a custom directory for exclusive use by login/logout scripts, writing to a database instead of the filesystem, or the mktemp command. mktemp uses a special system call to ensure exclusive access to files under /tmp.

More information on secure temporary file handling.



[ Reply to This | # ]
System sleep depending on login & logout
Authored by: leb on Jan 04, '05 11:49:48AM

A valid point to this security issue. In my case, the system is at home, so quite a secure environment.



[ Reply to This | # ]
System sleep depending on login & logout
Authored by: gshenaut on Jan 04, '05 10:16:18PM

Maybe I've missed something, but I see several problems with this approach. First, the system could easily be doing a scheduled backup or other maintenance activity when no one is logged in--this approach will disallow that. Also, what's wrong with the system sleeping when someone is logged in? If I understand the call to pmset here, even screen-dimming is being disabled while someone is logged in. This doesn't seem right to me.

My approach (on my powerbook) is to dim the screen & spin down the disk fairly liberally, but never to sleep the system except when running off battery power. Being logged in or not isn't a factor.

The "correct" approach, where programs that need to manipulate power management do so in a cooperative way, is pretty nontrivial, though. It really calls for a system resource that can be designed into new code or imposed on old code.

Greg Shenaut



[ Reply to This | # ]
System sleep depending on login & logout
Authored by: osxpounder on Jan 05, '05 05:18:00PM

Why wouldn't you want the machine to sleep when a user's logged in? Well, as the original poster said, during long downloads .... but also, if you do 3D graphics, video rendering or heavy-duty audio file rendering or similar crunching, you don't want your hard drives to go to sleep.

I assumed the benefit of using this hint is that you can use a special user when you want the computer to stay awake -- log in as that user and start rendering, etc. .... but wouldn't that mean having to install apps again for that user? What a pain that would be.

---
--
osxpounder



[ Reply to This | # ]
System sleep depending on login & logout
Authored by: gshenaut on Jan 06, '05 05:31:18PM
Yes, sometimes you don't want the system to sleep, but not having it sleep whenever someone is logged in defeats the whole purpose of the sleep timer. The hard disk is spun down when it hasn't been used recently, something that won't happen during a long download & doesn't matter anyway because it will spin right back up the next time it is accessed. If you are rendering something, the screen may dim or blank, that's true, but if you are there, just press fn and it will come back. If you aren't around, then it won't matter.

Greg Shenaut

[ Reply to This | # ]