Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

You can set your Mac OS X account to automatically lock the screen so that the password is required after a certain amount of time of inactivity, but can you do the same thing using a schedule (say at 5:30 PM every day)? Kind of like how you can have the computer turn on/off or go to/wake up from sleep at a certain time.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

You can use launchd to do this. Place the following xml into a new text file in ~/Library/LaunchAgents/ and call it something descriptive with a suffix of .plist. For example, mine is ~/Library/LaunchAgents/logoutAt1730.plist.

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>Logout At 5:30 PM</string>
        <key>ProgramArguments</key>
        <array>
            <string>/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession</string>
            <string>-suspend</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
            <integer>17</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
    </dict>
    </plist>

If you want it to run on the current power cycle (Eg you don't want to restart for this to take effect) use launchctl load ~/Library/LaunchAgents/logoutAt1730.plist to tell launchd about the new item. It should load automatically next time you login.

Use launchctl list and look for the label string (Logout at 5:30 PM) to validate that launchd knows about the item.

I have verified this works on my workstation. I don't know why cron doesn't.

share|improve this answer
1  
Yes, that worked, thanks! Just named the file suspend instead of logout. Now I just need to get user confirmation with a popup, if that's possible--I'll open another question: apple.stackexchange.com/q/49946/8699 –  newenglander Apr 28 '12 at 19:49
    
New question came up here, FYI: apple.stackexchange.com/q/51006/8699 –  newenglander May 10 '12 at 17:20
add comment

EDIT: Although I am not certain of why cron fails to work for this specific use-case, this answer is superceeded by the (currently more correct) answer using launchd.

Use crontab -e in the terminal application to add /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend to the Crontab at the appropriate time, like so (for 5:30 PM):

30 17 * * * /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

share|improve this answer
    
Thanks for the tip, it doesn't work for me, but that's another question. (Shouldn't it be 17 instead of 7 for 5 PM?) –  newenglander Apr 22 '12 at 7:48
    
Ah yeah I did it on the fly. Good eye. I just tried the /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend command by itself and it worked. what OS are you running? –  zwerdlds Apr 22 '12 at 17:12
    
Lion. The command itself worked by itself for me too, but I tried setting a crontab with a time a few minutes in the future and nothing happened when the specified time came. –  newenglander Apr 23 '12 at 18:36
    
Hmm, I'm experiencing the same issue. I'll look into it tonight. Perhaps it would work as a script? –  zwerdlds Apr 23 '12 at 18:56
    
No luck there. I think the user running the cronjobs (root) can't call suspend on another user, at least not with this command. –  newenglander Apr 24 '12 at 19:02
show 4 more comments

I wanted something similar, but instead of always locking at a specific time, I wanted to lock only on certain conditions. The script I used to check those conditions was triggered by cron, so I had the same issue.

When I tried CGSession -suspend in a cronjob, and noticed that when it was triggered, Console.app logged an error, saying only root and the current logged user could trigger a Fast User Switching. So my solution: run it as root.

So in my script, I used the following, to lock the computer:

sudo /usr/bin/osascript -e 'tell application "System Events" to do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"'

Convoluted? Yes. Working? Positive!

share|improve this answer
    
Using sudo .../CGSession -suspend directly (without using AppleScript) didn't work either then? –  patrix Apr 19 '13 at 17:45
    
Yeah, it didn't. No idea why. –  Guillaume Boudreau Apr 19 '13 at 18:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.