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.
3 Answers
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.
-
1Yes, 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 Apr 28, 2012 at 19:49
-
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
-
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?) Apr 22, 2012 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?– zwerdldsApr 22, 2012 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. Apr 23, 2012 at 18:36
-
Hmm, I'm experiencing the same issue. I'll look into it tonight. Perhaps it would work as a script?– zwerdldsApr 23, 2012 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. Apr 24, 2012 at 19:02
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!
-
Using
sudo .../CGSession -suspend
directly (without using AppleScript) didn't work either then?– nohillside ♦Apr 19, 2013 at 17:45 -