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

drn*_*sie 64 hibernate sleep

如何让我的 Ubuntu 10.10 桌面休眠或睡眠并让它在第二天“醒来”?

我见过可以在 Windows 上执行此操作的软件,因此在 Ubuntu 上并不难!

fos*_*dom 81

唤醒

您感兴趣的命令是rtcwake

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

测试

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

sudo rtcwake -u -s 60 -m mem
Run Code Online (Sandbox Code Playgroud)

这应该在恢复之前暂停计算机 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.
Run Code Online (Sandbox Code Playgroud)

暂停到已知时间

一个脚本(在这篇文章的底部)可用于暂停您的计算机并在特定时间唤醒:

语法是suspend_until [hh:mm]例如

sudo ./suspend_until 07:30
Run Code Online (Sandbox Code Playgroud)

将脚本保存为名称suspend_until并赋予它执行权限即

chmod +x suspend_until
Run Code Online (Sandbox Code Playgroud)

定时任务

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

sudo crontab -e
Run Code Online (Sandbox Code Playgroud)

现在输入类似在 23:30 运行挂起脚本的内容:

30 23 * * * /home/myhomefolder/suspend_until 07:30
Run Code Online (Sandbox Code Playgroud)

suspend_until 脚本

sudo rtcwake -u -s 60 -m mem
Run Code Online (Sandbox Code Playgroud)

NB

更改mem脚本的这一部分以使用适合您的挂起方法:

# Set RTC wakeup time
sudo rtcwake -l -m mem -t $DESIRED &
Run Code Online (Sandbox Code Playgroud)

您可能还需要根据您的硬件时钟是使用 UTC ( ) 还是本地 ( ) 时间来替换-u标志来代替标志。请注意,您的硬件时钟与您在操作系统中看到的系统时钟不同。-l-u-l

归功于redgeonline

  • killall 是不必要的,rtcwake 不作为守护进程运行,它只是将一个值写入 /sys/class/rtc/rtc0/wakealarm;再次运行它会将另一个值写入该文件。另外,去掉 rtcwake 末尾的 &,它会在完成后退出。然后你可以删除 sleep 命令。而且,如果您想在脚本中运行其他 root 命令,为什么不运行整个 sudo 而不是单个命令呢? (6认同)
  • 谢谢 - 只是稍微更新以突出显示您需要针对任何适合您的挂起方法更改脚本的哪一部分。 (3认同)

小智 5

我使用 rtcwake 创建了一个简单的 bash 脚本。它使用php将自然语言翻译成系统时间。例如:

  • sudo ./cu "tomorrow 9am"

  • sudo ./cu "next monday 3pm"

  • sudo ./cu "1 hour ago"

    rtcwake: time doesn't go backward

你可以在这里下载。

#!/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
Run Code Online (Sandbox Code Playgroud)

  • 请注意, date -d 已经理解几个这样的字符串:http://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html (4认同)