Linux:如何在睡眠一段时间后休眠

DrN*_*one 5 linux suspend

我在 linux 中缺少的 Windows 功能之一如下:在 Windows 中,您关闭笔记本电脑盖,系统暂停到 RAM,一段时间(可配置)后,计算机自动唤醒并继续暂停到磁盘。我知道在 linux 中存在 suspend2both 模式,但它可以在电池耗尽之前暂停到磁盘,这是我试图避免的。

编辑:使用答案数据搜索后找到更详细的答案

https://askubuntu.com/questions/12383/how-to-go-automatically-from-suspend-into-hibernate

EDIT2:这些是我在 MSI Wind U100 上使用 Ubuntu 11.04 遵循的步骤。

第一:我安装了 tuxonice,因为我的上网本无法进入休眠状态。作为副作用,休眠和唤醒过程非常快且非常稳定。唯一的缺点是休眠/恢复上的显示是文本模式。安装 tuxonice 最简单的方法是添加相应的 ppa:https ://launchpad.net/~tuxonice/+archive/ppa

一旦你进入休眠状态,这个脚本就会发挥所有作用

#!/bin/bash
# Script name: /etc/pm/sleep.d/00rtchibernate
# Purpose: Auto hibernates after a period of sleep
# Edit the "autohibernate" variable below to set the number of seconds to sleep.
curtime=$(date +%s)
autohibernate=7200
echo "$curtime $1" >>/tmp/autohibernate.log
if [ "$1" = "suspend" ]
then
    # Suspending.  Record current time, and set a wake up timer.
    echo "$curtime" >/var/run/pm-utils/locks/rtchibernate.lock
    rtcwake -m no -s $autohibernate
fi

if [ "$1" = "resume" ]
then
    # Coming out of sleep
    sustime=$(cat /var/run/pm-utils/locks/rtchibernate.lock)
    rm /var/run/pm-utils/locks/rtchibernate.lock
    # Did we wake up due to the rtc timer above?
    if [ $(($curtime - $sustime)) -ge $autohibernate ]
    then
        # Then hibernate
        rm /var/run/pm-utils/locks/pm-suspend.lock
        /usr/sbin/pm-hibernate
    else
        # Otherwise cancel the rtc timer and wake up normally.
        rtcwake -m no -s 1
    fi
fi
Run Code Online (Sandbox Code Playgroud)

通过修改自动休眠值,您可以更改机器唤醒并立即进入休眠状态的睡眠时间

注意:您可能需要安装 rtcwake,我已经安装了,但我不记得是否自己安装了该软件包。

小智 4

对于 CentOS 或 Fedor 或 Redhat 等使用systemd. 我们需要更改脚本的位置,而不是将其放置在适当的/etc/pm/sleep.d/位置/usr/lib/systemd/system-sleep/以使其可执行。0000rtchibernate.shchmod +x

最后,脚本中还需要进行一些调整才能使其与systemd. 为了简单起见,我给出了完整的重写脚本

#!/bin/bash
# Script name: /usr/lib/systemd/system-sleep/0000rtchibernate
# Purpose: Auto hibernates after a period of sleep
# Edit the "autohibernate" variable below to set the number of seconds to sleep.
curtime=$(date +%s)
autohibernate=3600 #number is second
lock=/tmp/rtchibernate.lock
echo "$curtime $1" >>/tmp/autohibernate.log
if [ "$1" = "pre" ]
then
    # Suspending.  Record current time, and set a wake up timer.
    echo "$curtime" > $lock
    rtcwake -m no -s $autohibernate
fi

if [ "$1" = "post" ]
then
    # Coming out of sleep
    sustime=`cat $lock`
    rm $lock
    # Did we wake up due to the rtc timer above?
    if [ $(($curtime - $sustime)) -ge $autohibernate ]
    then
        # Then hibernate
        systemctl hibernate
    else
        # Otherwise cancel the rtc timer and wake up normally.
        rtcwake -m no -s 1
    fi
fi
Run Code Online (Sandbox Code Playgroud)

变量autohibernate在几秒钟内改变它看起来合适。我希望我有帮助