When connecting to an OS X box via SSH, you may want to put it to sleep after you’re done.
This is no system call to put the computer to sleep that I know of. However, Applescript can do it, and it is trivial to call the OSAScript interpreter in bash.
The following script puts the computer to sleep:
#!/bin/bash osascript -e 'tell application "System Events" to sleep'
Edit: found a better way!
I don’t know if this is specific to Snow Leopard, but the following command will work as well without having to use Applescript or administrator rights:
pmset sleepnow
there is: sudo shutdown -s now
to not have to enter password you have to add a line with: sudo visudo
Hey, that’s neat! I didn’t know that option was in shutdown at all. :)
I like the fact that Applescript doesn’t require to be superuser, but using the system’s “shutdown” command might be more appealing to some people. Thanks for the tip!
with -h you halt and -r reboot
instead of now you can specify a time
visudo allow to grant users access to sudo commands
without be a sudouser
for example if you add :
%users ALL=/sbin/shutdown
then every users can use shutdown from everywhere
%admin ALL=/sbin/shutdown
grant to admin
me localhost=/sbin/shutdown
grant to user me from localhost
“pmset sleepnow”
Works great for Leopard as well.
Awesome, thanks!
“osascript -e ‘tell application “System Events” to sleep’ ” also works for Leopard.
you also can use
pmset sleepnow
More interesting if somebody knows how to hibernate computer without changing hibernate mode?
Looks like I found the solution. For instance:
sudo pmset -a hibernatemode 1 force
pmset sleepnow
Parameter force changes settings without saving it on to the disk. So, next time hibernatemode will be the previous one.
note that if you are remotely logged in, it won’t sleep with this or any command.
To see the reason it won’t sleep run
pmset -g assertions
then look if NetworkClientActive is “1” (or any of the other reasons).
so to do it remotely you could do
nohup /bin/sh -c ‘/bin/sleep 10 && pmset sleepnow’ &
then logout inside 10 seconds.
thanks justin! exactly what I was searching for ;)
Actually, if you exit your remote session immediately it will. E.g. This just worked for me via ssh:
pmset sleepnow && exit
No. This command does not work. The â â are wrong.