最近公司規(guī)定晚上走人后必須關(guān)閉電腦,但是像我們這樣的人,經(jīng)常會(huì)忘記了關(guān)閉電腦,而且關(guān)閉電腦之后再恢復(fù)工作環(huán)境也是件挺麻煩的事情,無奈之下只能折騰一下,讓linux定時(shí)休眠了。
10年積累的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有常德免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
休眠的類型
目前大概由三種類型的休眠:
suspend(suspend to RAM)
指的是除了內(nèi)存以外的大部分機(jī)器部件都進(jìn)入斷電狀態(tài)。 這種休眠狀態(tài)恢復(fù)速度特別快,但由于內(nèi)存中的數(shù)據(jù)并沒有被保存下來,因此這個(gè)狀態(tài)的系統(tǒng)并沒有進(jìn)入真正意義上的休眠狀態(tài),還在持續(xù)耗電。
hibernate(suspend to disk)
這種休眠會(huì)將內(nèi)存中的系統(tǒng)狀態(tài)寫入交換空間內(nèi),當(dāng)系統(tǒng)啟動(dòng)時(shí)就可以從交換空間內(nèi)讀回系統(tǒng)狀態(tài)。 這種情況下系統(tǒng)可以完全斷電,但由于要保存/讀取系統(tǒng)狀態(tài)到/從交換空間,因此速度會(huì)比較慢,而且需要進(jìn)行一些配置(下面會(huì)說到)
hybrid(suspend to both)
結(jié)合了上面兩種休眠類型。它像hibernate一樣將系統(tǒng)狀態(tài)存入交換空間內(nèi),同時(shí)也像suspend一樣并不關(guān)閉電源。 這種,在電源未耗盡之前,它能很快的從休眠狀態(tài)恢復(fù)。而若休眠期間電源耗盡,則它可以從交換空間中恢復(fù)系統(tǒng)狀態(tài)。
suspend 休眠
進(jìn)入 suspend 特別簡單,無需額外的配置,在 systemd 系統(tǒng)上直接執(zhí)行 systemctl suspend 就行了。\
systemctl suspend
它的實(shí)際動(dòng)作由 systemd-suspend.service 所定義, 在 archlinux 上,它長成這樣子的:
# SPDX-License-Identifier: LGPL-2.1+ ## This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. [Unit] Description=Suspend Documentation=man:systemd-suspend.service(8) DefaultDependencies=no Requires=sleep.target After=sleep.target [Service] Type=oneshot ExecStart=/usr/lib/systemd/systemd-sleep suspend
Hibernation 休眠
由于 hibernation 休眠要求將內(nèi)存中的內(nèi)容寫入到交換空間中,因此你至少要有一個(gè)空間大于內(nèi)存的交換分區(qū)或者交換文件。 (其實(shí)若交換空間不夠內(nèi)存大也不是一定就無法進(jìn)行hibernation休眠,可以嘗試運(yùn)行 echo 0 |sudo tee /sys/power/image_size, 這會(huì)讓系統(tǒng)在寫入交換空間時(shí)盡可能的進(jìn)行壓縮,但這種方法也無法保證一定能夠休眠成功)
若之前沒有創(chuàng)建交換分區(qū),那么可以臨時(shí)創(chuàng)建一個(gè)交換文件來用。比如下面命令創(chuàng)建一個(gè)5G的交換文件
sudo dd if=/dev/zero of=/swapfile bs=10240 count=524288 sudo mkswap /swapfile sudo chmod 0600 /swapfile sudo swapon /swapfile sudo cp /etc/fstab /etc/fstab.bak echo "/swapfile swap swap default 0 0" |tee -a /etc/fstab Setting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=d0f0c682-e1fa-416f-8fe2-b554b8ca363a /swapfile swap swap default 0 0
除此創(chuàng)建交換分區(qū)之外,我們還需要修改kernel的啟動(dòng)參數(shù),讓系統(tǒng)在啟動(dòng)時(shí)先嘗試從交換空間中恢復(fù)狀態(tài)。 具體操作如下:
1. 如果使用交換分區(qū)來保存,則只需要為添加kernel的啟動(dòng)參數(shù) resume=交換分區(qū) 即可
(1) 查看那塊分區(qū)是交換分區(qū)
swapon
NAME TYPE SIZE USED PRIO /dev/sda2 partition 8G 280K -2 /swapfile file 5G 0B -3
可以看出交換分區(qū)為 /dev/sda2
(2) 修改 /etc/default/grub, 為 GRUB_CMDLINE_LINUX_DEFAULT 行添加參數(shù) resume=/dev/sda2
sudo sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/ s!"$! resume=/dev/sda2"!' /etc/default/grub
2. 如果是使用交換文件,則需要添加兩個(gè)參數(shù) resume=交換文件所在磁盤 以及 resume_offset=交換文件在磁盤中的偏移位置:
(1) 查看交換文件所在磁盤
df /swapfile
文件系統(tǒng) 1K-塊 已用 可用 已用% 掛載點(diǎn)
/dev/sda3 55253696 27582224 24834972 53% /
說明磁盤為 /dev/sda3
(2) 查看交換文件的偏移位置
sudo filefrag -v /swapfile|head -5 Filesystem type is: ef53 File size of /swapfile is 5368709120 (1310720 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 32767: 4653056.. 4685823: 32768: 1: 32768.. 65535: 4685824.. 4718591: 32768:
這里可以看出物理偏移位置時(shí)4653056
(3) 修改 /etc/default/grub, 為 GRUB_CMDLINE_LINUX_DEFAULT 行添加參數(shù) resume=/dev/sda3 resume_offset=4653056
sudo sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/ s!"$! resume=/dev/sda3 resume_offset=4653056"!' /etc/default/grub
3. 重新生成 grub.cfg 文件
sudo grub-mkconfig -o /boot/grub/grub.cfg
(1) 配置initramfs添加 resume hook 修改 /etc/mkinitcpio.conf 文件,在 HOOKS 中添加 resume
sudo sed -i '/^HOOKS=/ s/)/ resume)/' /etc/mkinitcpio.conf
其中由兩點(diǎn)需要注意:
(2) 重新生成initramfs
sudo mkinitcpio -g /boot/initramfs-linux-lily.img ==> Starting build: 4.16.12-2-lily -> Running build hook: [base] -> Running build hook: [udev] -> Running build hook: [autodetect] -> Running build hook: [modconf] -> Running build hook: [block] -> Running build hook: [filesystems] -> Running build hook: [keyboard] -> Running build hook: [fsck] -> Running build hook: [resume] ==> Generating module dependencies ==> Creating gzip-compressed initcpio image: /boot/initramfs-linux-lily.img ==> Image generation successful
(3) 重啟,讓配置生效
經(jīng)過上面復(fù)雜的配置后,hibernation 休眠才能真正成功。與 suspend 休眠類似,我們也能使用 systemctl 來進(jìn)行休眠
systemctl hibernate
類似的,它的實(shí)際動(dòng)作由 systemd-hibernte.service 所定義, 在 archlinux 上,它長成這樣子的:
# SPDX-License-Identifier: LGPL-2.1+ # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. [Unit] Description=Hibernate Documentation=man:systemd-suspend.service(8) DefaultDependencies=no Requires=sleep.target After=sleep.target [Service] Type=oneshot ExecStart=/usr/lib/systemd/systemd-sleep hibernate
hybrid 休眠
在配置好 hibernate 休眠后,也就能正常進(jìn)行 hybrid 休眠了,方法是執(zhí)行
systemctl hybrid-sleep
類似的,它的實(shí)際動(dòng)作由 systemd-hybrid-sleep.service 所定義, 在 archlinux 上,它長成這樣子的:
# SPDX-License-Identifier: LGPL-2.1+ # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. [Unit] Description=Hybrid Suspend+Hibernate Documentation=man:systemd-suspend.service(8) DefaultDependencies=no Requires=sleep.target After=sleep.target [Service] Type=oneshot ExecStart=/usr/lib/systemd/systemd-sleep hybrid-sleep
Sleep Hooks
從上面的service文件中可以看出,不管是哪種類型的系統(tǒng)休眠,其內(nèi)部實(shí)際調(diào)用的都是 systemd-sleep.
man systemd-sleep
SYSTEMD-SUSPEND.SERVICE(8) systemd-suspend.service SYSTEMD-SUSPEND.SERVICE(8) NAME systemd-suspend.service, systemd-hibernate.service, systemd-hybrid- sleep.service, systemd-sleep - System sleep state logic SYNOPSIS systemd-suspend.service systemd-hibernate.service systemd-hybrid-sleep.service /usr/lib/systemd/system-sleep DESCRIPTION systemd-suspend.service is a system service that is pulled in by suspend.target and is responsible for the actual system suspend. Similarly, systemd-hibernate.service is pulled in by hibernate.target to execute the actual hibernation. Finally, systemd-hybrid-sleep.service is pulled in by hybrid-sleep.target to execute hybrid hibernation with system suspend. Immediately before entering system suspend and/or hibernation systemd-suspend.service (and the other mentioned units, respectively) will run all executables in /usr/lib/systemd/system-sleep/ and pass two arguments to them. The first argument will be "pre", the second either "suspend", "hibernate", or "hybrid-sleep" depending on the chosen action. Immediately after leaving system suspend and/or hibernation the same executables are run, but the first argument is now "post". All executables in this directory are executed in parallel, and execution of the action is not continued until all executables have finished. Note that scripts or binaries dropped in /usr/lib/systemd/system-sleep/ are intended for local use only and should be considered hacks. If applications want to react to system suspend/hibernation and resume, they should rather use the Inhibitor interface[1]. Note that systemd-suspend.service, systemd-hibernate.service, and systemd-hybrid-sleep.service should never be executed directly. Instead, trigger system sleep states with a command such as "systemctl suspend" or similar. Internally, this service will echo a string like "mem" into /sys/power/state, to trigger the actual system suspend. What exactly is written where can be configured in the "[Sleep]" section of /etc/systemd/sleep.conf or a sleep.conf.d file. See systemd- sleep.conf(5). OPTIONS systemd-sleep understands the following commands: -h, --help Print a short help text and exit. --version Print a short version string and exit. suspend, hibernate, hybrid-sleep Suspend, hibernate, or put the system to hybrid sleep. SEE ALSO systemd-sleep.conf(5), systemd(1), systemctl(1), systemd.special(7), systemd-halt.service(8) NOTES 1. Inhibitor interface
https://www.freedesktop.org/wiki/Software/systemd/inhibit
systemd 238 SYSTEMD-SUSPEND.SERVICE(8)
根據(jù) systemd-sleep 的manual pages,可以看到在系統(tǒng)休眠之前以及從休眠狀態(tài)恢復(fù)之后,都會(huì)并行地調(diào)用 /usr/lib/systemd/system-sleep 中的腳本,并傳遞兩個(gè)參數(shù)。
第一個(gè)參數(shù)用來指定是開始休眠還是從休眠狀態(tài)恢復(fù),分別對應(yīng)的字符串 “pre” 與 “post”.
第二個(gè)參數(shù)用來指明休眠的類型,分別為字符串 “suspend”, “hibernate” 以及 “hybrid-sleep”
定時(shí)執(zhí)行休眠
systemd 系統(tǒng)中的定時(shí)任務(wù)是由timer來實(shí)現(xiàn)的,而每個(gè)timer都與一個(gè)service相對應(yīng)。
一般情況下,timer的名稱與service一致,但必要時(shí)可以通過在.timer文件中的 [Timer] 部分指定 Unit= 選項(xiàng)來控制一個(gè)與timer不同名的service。
下面是一個(gè)timer的例子,每天21:30分開始自動(dòng)hibernate休眠
[Unit] Description=Hibernate every 21:30:00 [Timer] OnCalendar=*-*-* 21:30:00 Persistent=true Unit=systemd-hibernate.service [Install] WantedBy=timers.target
定時(shí)喚醒休眠的linux
使用 rtcwake 可以在給定的時(shí)間喚醒處于休眠狀態(tài)的電腦
其主要用法為:
sudo rtcwake -m ${mode} -t ${time_t} # 或者 sudo rtcwake -m ${mode} -s ${seconds}
其中,參數(shù)mode為待機(jī)模式,有以下幾個(gè)選項(xiàng):
standby
普通待機(jī)模式,為默認(rèn)選項(xiàng),對應(yīng) ACPI state S1
mem
suspend休眠,對應(yīng) ACPI state S3
disk
hibernation 休眠,對應(yīng) ACPI state S4
off
通過調(diào)用系統(tǒng)的關(guān)機(jī)命令來休眠,對應(yīng) ACPI state S5
參數(shù) time_t 為從 1970-01-01, 00:00 UTC 開始到現(xiàn)在的秒數(shù),可以通過 date 命令來將時(shí)間字符串轉(zhuǎn)換成這個(gè)秒數(shù),比如
sudo rtcwake -m disk -t $(date -d 08:30 +%s)
就是進(jìn)行 hibernation 休眠,并于08:30分喚醒
參數(shù) seconds 為秒數(shù),表示從現(xiàn)在開始的多少秒后,系統(tǒng)喚醒。
總結(jié)
以上所述是小編給大家介紹的linux 定時(shí)休眠的實(shí)現(xiàn)思路,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
本文題目:linux定時(shí)休眠的實(shí)現(xiàn)思路
網(wǎng)站鏈接:http://aaarwkj.com/article8/gjdiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、動(dòng)態(tài)網(wǎng)站、域名注冊、、定制開發(fā)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)