自动在特定时间休眠和唤醒。

如何在Ubuntu 10.10桌面上让电脑进入休眠或睡眠状态,并在第二天“唤醒”呢?
我在Windows上见过可以做到这一点的软件,所以在Ubuntu上应该也不难!
4个回答

rtcwake

您感兴趣的命令是rtcwake

此程序用于进入系统睡眠状态,直到指定的唤醒时间。

测试

要找到适合您的正确语法,请尝试以下操作:

sudo rtcwake -u -s 60 -m mem

这将暂停计算机60秒钟,然后恢复。重要的参数是mem。您有几个选项可以选择-尝试找到最适合您的值。
          standby
                 ACPI state S1. This state offers  minimal,  though  real,
                 power savings, while providing a very low-latency transi‐
                 tion back to a working system. This is the default mode.

          mem    ACPI state S3 (Suspend-to-RAM). This state offers signif‐
                 icant  power  savings  as everything in the system is put
                 into a low-power  state,  except  for  memory,  which  is
                 placed in self-refresh mode to retain its contents.

          disk   ACPI  state  S4  (Suspend-to-disk). This state offers the
                 greatest power savings, and  can  be  used  even  in  the
                 absence  of  low-level platform support for power manage‐
                 ment. This state operates  similarly  to  Suspend-to-RAM,
                 but  includes  a final step of writing memory contents to
                 disk.

          off    ACPI  state  S5  (Poweroff).  This  is  done  by  calling
                 '/sbin/shutdown'.   Not officially supported by ACPI, but
                 usually working.

          no     Don't suspend. The rtcwake command sets RTC  wakeup  time
                 only.

          on     Don't  suspend,  but  read  RTC  device  until alarm time
                 appears. This mode is useful for debugging.

暂停直到已知时间

可以使用以下脚本(在本帖底部)来暂停您的计算机并在特定时间唤醒:

语法为suspend_until [hh:mm],例如

sudo ./suspend_until 07:30

将脚本保存为名称suspend_until并赋予执行权限,即:
chmod +x suspend_until

定时任务

您可以创建一个根定时任务,调用此脚本在晚上的特定时间执行,然后在早上唤醒:

sudo crontab -e

现在输入类似于在23:30运行挂起脚本的内容。
30 23 * * * /home/myhomefolder/suspend_until 07:30

暂停直到脚本

#!/bin/bash

# Auto suspend and wake-up script
#
# Puts the computer on standby and automatically wakes it up at specified time
#
# Written by Romke van der Meulen <redge.online@gmail.com>
# Minor mods fossfreedom for AskUbuntu
#
# Takes a 24hour time HH:MM as its argument
# Example:
# suspend_until 9:30
# suspend_until 18:45

# ------------------------------------------------------
# Argument check
if [ $# -lt 1 ]; then
    echo "Usage: suspend_until HH:MM"
    exit
fi

# Check whether specified time today or tomorrow
DESIRED=$((`date +%s -d "$1"`))
NOW=$((`date +%s`))
if [ $DESIRED -lt $NOW ]; then
    DESIRED=$((`date +%s -d "$1"` + 24*60*60))
fi

# Kill rtcwake if already running
sudo killall rtcwake

# Set RTC wakeup time
# N.B. change "mem" for the suspend option
# find this by "man rtcwake"
sudo rtcwake -l -m mem -t $DESIRED &

# feedback
echo "Suspending..."

# give rtcwake some time to make its stuff
sleep 2

# then suspend
# N.B. dont usually require this bit
#sudo pm-suspend

# Any commands you want to launch after wakeup can be placed here
# Remember: sudo may have expired by now

# Wake up with monitor enabled N.B. change "on" for "off" if 
# you want the monitor to be disabled on wake
xset dpms force on

# and a fresh console
clear
echo "Good morning!"

注意:

在脚本的这部分更改mem为适合您的任何挂起方法:

# Set RTC wakeup time
sudo rtcwake -l -m mem -t $DESIRED &

你可能还需要根据你的硬件时钟是使用UTC(-u)还是本地时间(-l),来替换-l标志。请注意,你的硬件时钟与操作系统中显示的系统时钟不同。
credit to redgeonline

1我所要求的...以及更多!感谢您在字里行间的阅读! - drnessie
3谢谢 - 只是稍微更新一下,以突出脚本中需要更改的部分,以适应您所选择的任何暂停方法。 - fossfreedom
6killall是不必要的,rtcwake不作为一个守护进程运行,它只是将一个值写入/sys/class/rtc/rtc0/wakealarm;再次运行它会向该文件写入另一个值。同时,在rtcwake的末尾去掉&,当它完成任务时就会退出。然后你可以删除sleep命令。另外,如果你想在脚本中运行其他root命令,为什么不直接以sudo方式运行整个脚本呢? - unhammer
我的旧笔记本电脑无法使用上述脚本唤醒。所以我做了以下操作:

root crontab

30 20 * * * /home/gare/Documents/scripts/suspend_10_hours.sh >> /home/gare/Documents/scripts/suspend.log~$ more /home/gare/Documents/scripts/suspend_10_hours.sh #!/bin/bash

旧笔记本电脑无法唤醒,因此告诉rtcwake暂停10小时

10小时60分钟60秒=36000

sudo rtcwake -u -s 36000 -m mem
- gare
1除了@unhammer所说的之外,killall不仅是多余的,而且实际上是有害的,因为它会向系统上所有与"killall"匹配的进程发送SIGTERM信号。请采纳unhammer提出的修复建议,而不是导致人们复制错误的做法。 - josch
根据您的硬件时钟是使用UTC(-u)还是本地(-l)时间,情况可能有所不同。要确定这一点并不直接明显。尽管我怀疑除了英国/格林威治标准时间以外的几乎所有人都可能想使用-lsudo hwclock --show并不能明确告诉您,尽管我猜如果您需要使用UTC,它应该会在某个地方显示UTC。 - icc97

使用rtcwake,我创建了一个简单的bash脚本。它使用php将自然语言翻译成系统时间。例如:
  • sudo ./cu "明天上午9点"

  • sudo ./cu "下周一下午3点"

  • sudo ./cu "1小时前"

    rtcwake:时间不能倒退

你可以在这里下载它。

#!/bin/bash
export sdate=$1

date=`/usr/bin/php << 'EOF'
<?php
date_default_timezone_set("Etc/GMT-2");
$date = strtotime(GETENV("sdate"));
echo "\r".$date;
EOF`

rtcwake -m mem -t $date

4请注意,date -d已经可以理解几种这样的字符串:http://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html - unhammer

rtcwake对我的机器没有效果。 在我的华硕主板上,我必须在BIOS中设置唤醒时间。 我在高级> APM菜单下找到了该设置,并且即使我的BIOS时间设置为美国东部时间,我也必须使用协调世界时。


太棒了,谢谢分享。我稍微添加了一点内容,这样它就会在晚上11点睡觉,早上8点醒来。
sudo until [[ $(date +%H:%M) == 23:00 ]]; do sleep 2; done; rtcwake -u -s 32400 -m mem

bash: syntax error near unexpected token 'do' - undefined