Ubuntu20.04启用休眠-hibernate
# Ubuntu20.04启用休眠-hibernate
# 在系统中启用休眠功能
方法
I assume you have a swap partition ready to use. (If you have a swap file you cannot hibernate)
Install pm-utils
and hibernate
:
sudo apt install pm-utils hibernate
Then:
cat /sys/power/state
You should see:
freeze mem disk
Then run:
grep swap /etc/fstab
Copy the UUID value. You will need it later. Then run:
code /etc/default/grub
(or your favorite editor if not VScode). Change the line that says
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
so that it instead says:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=YOUR_VALUE"
Then, after saving the file and quitting the text editor, run:
sudo update-grub
To test it, run:
sudo systemctl hibernate
Right now no GNOME extension is working to have the hibernate option back in the system menu.
how-to-enable-hibernate-option-in-ubuntu-20-04 (opens new window)
确保 Swap 分区要比内存容量要大
Swap 分区的相关设置
- 新建 Swap 分区 (opens new window)
- 如果 Swap 分区已存在, 则需先运行
sudo swapoff /swapfile
来关闭当前分区
重启后黑屏的解决办法
添加
resumedelay
选项,以确保swap file
有充足的时间变得可读写GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=27c000dd-892e-4aaa-b938-349c13e3f50c resumedelay=8"
1之后,更新
grub
sudo update-grub
1
configuring-lubuntu-18-04-to-enable-hibernation-using-a-swap-file (opens new window)
# 添加 hibernate 选项至关机界面
方法
使用 gnome 插件 simpler-off-menu (opens new window)
在其设置界面即可启
hibernate
选项- 设置完成后需要重启 gnome 桌面
# 从休眠状态恢复后不需要密码就能显示桌面的解决方案
解决问题思路
- 刚开始的思路是修改系统服务,但是发现用于锁屏的命令
gnome-screensaver-command -l
总是提示跟ibus
相关的错误,暂时未找到解决方案- 可能是
environment
设置的问题 ⌛tag+todo
- 可能是
- 所以,中间就考虑用脚本解决
- 最后发现也可以改写
gnome-extension
,使其中的hibernate
按钮在休眠状态恢复后进入锁屏
提示
为了使用命令行锁屏,首先需要安装 gnome-screensaver
sudo apt-get install gnome-screensaver
# 用脚本进行休眠
脚本文件如下
#!/bin/bash
echo -e
echo "----------进入休眠状态----------"
sudo /usr/bin/gnome-screensaver-command --lock &&
sudo systemctl hibernate &&
echo "----------从休眠状态恢复---------"
2
3
4
5
6
- 可以使用
alias
命令在~/.zshrc
中自定义休眠所用的命令- 如
alias hibernate='~/OneDrive/SyncFile/scripts/shell/hibernate.sh'
- 如
# 修改 simpler-off-menu, 使得 hibernate
按钮也有相同的功能
解决步骤
gnome extension
的本地文件夹在
~/.local/share/gnome-shell/extensions/SimplerOffMenu.kerkus@pm.me/
修改
extension.js
, 将其FuncHibernate
函数改为如下形式function FuncHibernate(widget, event) { Main.overview.hide(); try { Util.spawn([ "/bin/bash", "-c", "systemctl hibernate && /usr/bin/gnome-screensaver-command -l", ]); } catch (err) { Main.notify("Error " + err); } }
1
2
3
4
5
6
7
8
9
10
11
12
修改完成后,按
Alt+F2
输入r
重启gnome
后即可生效