Sleep but not Logout

Gennaro
New Contributor III

Hello,

Does anybody have a solution to have the machine fall asleep whilst not logging out of the currently logged in user?

5 REPLIES 5

mark_mahabir
Valued Contributor

Are your Macs encrypted with FileVault 2?

ryan_ball
Valued Contributor
#!/bin/bash
loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
sudo -u "$loggedInUser" osascript -e 'tell app "System Events" to sleep'

joshuasee
Contributor III
pmset sleepnow

Gennaro
New Contributor III

@ryan.ball Hey Ryan, I gave your script a shot, seems as though it does put the computer to sleep, but still logs the user off at the same time. Any suggestions?

ryan_ball
Valued Contributor

@Gennaro Is it logging the user out, or is your "Security & Privacy" preference pane in System Preferences set to "Require password immediately after sleep or screen saver begins"?