欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux 定時休眠的實現(xiàn)思路

 更新時間:2018年07月19日 11:11:05   作者:ImportNew  
這篇文章主要介紹了linux 定時休眠的實現(xiàn)思路,休眠的類型有三種類型,文章對每種類型介紹的都很詳細,需要的朋友可以參考下

最近公司規(guī)定晚上走人后必須關(guān)閉電腦,但是像我們這樣的人,經(jīng)常會忘記了關(guān)閉電腦,而且關(guān)閉電腦之后再恢復(fù)工作環(huán)境也是件挺麻煩的事情,無奈之下只能折騰一下,讓linux定時休眠了。

休眠的類型

目前大概由三種類型的休眠:

suspend(suspend to RAM)

指的是除了內(nèi)存以外的大部分機器部件都進入斷電狀態(tài)。 這種休眠狀態(tài)恢復(fù)速度特別快,但由于內(nèi)存中的數(shù)據(jù)并沒有被保存下來,因此這個狀態(tài)的系統(tǒng)并沒有進入真正意義上的休眠狀態(tài),還在持續(xù)耗電。

hibernate(suspend to disk)

這種休眠會將內(nèi)存中的系統(tǒng)狀態(tài)寫入交換空間內(nèi),當系統(tǒng)啟動時就可以從交換空間內(nèi)讀回系統(tǒng)狀態(tài)。 這種情況下系統(tǒng)可以完全斷電,但由于要保存/讀取系統(tǒng)狀態(tài)到/從交換空間,因此速度會比較慢,而且需要進行一些配置(下面會說到)

hybrid(suspend to both)

結(jié)合了上面兩種休眠類型。它像hibernate一樣將系統(tǒng)狀態(tài)存入交換空間內(nèi),同時也像suspend一樣并不關(guān)閉電源。 這種,在電源未耗盡之前,它能很快的從休眠狀態(tài)恢復(fù)。而若休眠期間電源耗盡,則它可以從交換空間中恢復(fù)系統(tǒng)狀態(tài)。

suspend 休眠

進入 suspend 特別簡單,無需額外的配置,在 systemd 系統(tǒng)上直接執(zhí)行 systemctl suspend 就行了。\

systemctl suspend

它的實際動作由 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)容寫入到交換空間中,因此你至少要有一個空間大于內(nèi)存的交換分區(qū)或者交換文件。 (其實若交換空間不夠內(nèi)存大也不是一定就無法進行hibernation休眠,可以嘗試運行 echo 0 |sudo tee /sys/power/image_size, 這會讓系統(tǒng)在寫入交換空間時盡可能的進行壓縮,但這種方法也無法保證一定能夠休眠成功)

若之前沒有創(chuàng)建交換分區(qū),那么可以臨時創(chuàng)建一個交換文件來用。比如下面命令創(chuàng)建一個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的啟動參數(shù),讓系統(tǒng)在啟動時先嘗試從交換空間中恢復(fù)狀態(tài)。 具體操作如下:

1. 如果使用交換分區(qū)來保存,則只需要為添加kernel的啟動參數(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. 如果是使用交換文件,則需要添加兩個參數(shù) resume=交換文件所在磁盤 以及 resume_offset=交換文件在磁盤中的偏移位置:

(1) 查看交換文件所在磁盤

df /swapfile

文件系統(tǒng)          1K-塊     已用     可用 已用% 掛載點
/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:

這里可以看出物理偏移位置時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

其中由兩點需要注意:

  • 由于分區(qū)的label和UUID都是 udev 分配的,因此 resume 必須放在 udev 之后
  • 由于 systemd hook 已經(jīng)有了 resume 的功能,因此若已經(jīng)有了 systemd hook,則無需再添加 udev hook

(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 來進行休眠

systemctl hibernate

類似的,它的實際動作由 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 休眠后,也就能正常進行 hybrid 休眠了,方法是執(zhí)行

systemctl hybrid-sleep

類似的,它的實際動作由 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)部實際調(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ù)之后,都會并行地調(diào)用 /usr/lib/systemd/system-sleep 中的腳本,并傳遞兩個參數(shù)。

第一個參數(shù)用來指定是開始休眠還是從休眠狀態(tài)恢復(fù),分別對應(yīng)的字符串 “pre” 與 “post”.

第二個參數(shù)用來指明休眠的類型,分別為字符串 “suspend”, “hibernate” 以及 “hybrid-sleep”

定時執(zhí)行休眠

systemd 系統(tǒng)中的定時任務(wù)是由timer來實現(xiàn)的,而每個timer都與一個service相對應(yīng)。

一般情況下,timer的名稱與service一致,但必要時可以通過在.timer文件中的 [Timer] 部分指定 Unit= 選項來控制一個與timer不同名的service。

下面是一個timer的例子,每天21:30分開始自動hibernate休眠

[Unit]
Description=Hibernate every 21:30:00
[Timer]
OnCalendar=*-*-* 21:30:00
Persistent=true
Unit=systemd-hibernate.service
[Install]
WantedBy=timers.target

定時喚醒休眠的linux

使用 rtcwake 可以在給定的時間喚醒處于休眠狀態(tài)的電腦

其主要用法為:

sudo rtcwake -m ${mode} -t ${time_t}
# 或者
sudo rtcwake -m ${mode} -s ${seconds}

其中,參數(shù)mode為待機模式,有以下幾個選項:

standby

普通待機模式,為默認選項,對應(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)機命令來休眠,對應(yīng) ACPI state S5

參數(shù) time_t 為從 1970-01-01, 00:00 UTC 開始到現(xiàn)在的秒數(shù),可以通過 date 命令來將時間字符串轉(zhuǎn)換成這個秒數(shù),比如

sudo rtcwake -m disk -t $(date -d 08:30 +%s)

就是進行 hibernation 休眠,并于08:30分喚醒

參數(shù) seconds 為秒數(shù),表示從現(xiàn)在開始的多少秒后,系統(tǒng)喚醒。

總結(jié)

以上所述是小編給大家介紹的linux 定時休眠的實現(xiàn)思路,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 教你使用Apache搭建Http下載服務(wù)器

    教你使用Apache搭建Http下載服務(wù)器

    這篇文章主要介紹了使用Apache搭建Http下載服務(wù)器的詳細過程,Apache2默認采用的是80端口號,因此直接通過公網(wǎng)ip或域名就能訪問,需要的朋友可以參考下
    2022-10-10
  • Linux性能監(jiān)控工具nmon安裝及使用教程解析

    Linux性能監(jiān)控工具nmon安裝及使用教程解析

    這篇文章主要介紹了Linux性能監(jiān)控工具nmon安裝及使用教程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Linux下如何對端口流量進行統(tǒng)計

    Linux下如何對端口流量進行統(tǒng)計

    本篇文章主要介紹了Linux下如何對端口流量進行統(tǒng)計,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 詳解linux dma驅(qū)動編寫

    詳解linux dma驅(qū)動編寫

    這篇文章主要介紹了詳解linux dma驅(qū)動編寫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 圖文詳解Linux服務(wù)器搭建JDK環(huán)境

    圖文詳解Linux服務(wù)器搭建JDK環(huán)境

    這篇文章主要以圖文結(jié)合的方式詳細介紹了Linux服務(wù)器搭建JDK環(huán)境的過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Ubuntu下pycharm無法導入類的解決方法

    Ubuntu下pycharm無法導入類的解決方法

    下面小編就為大家分享一篇Ubuntu下pycharm無法導入類的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Linux下Apache服務(wù)的部署和配置

    Linux下Apache服務(wù)的部署和配置

    這篇文章介紹了Linux下Apache服務(wù)的部署和配置,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • Centos8搭建基于kdc加密的nfs

    Centos8搭建基于kdc加密的nfs

    大家好,本篇文章主要講的是Centos8搭建基于kdc加密的nfs,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • ubuntu mysql更改tmp路徑的方法

    ubuntu mysql更改tmp路徑的方法

    因為mysql經(jīng)常要把文件定入tmp目錄,而tmp的目錄為/tmp,為了提高性能,所以考慮更改tmpdir目錄為/run/shm,即使用內(nèi)存來存儲。下文給大家介紹ubuntu mysql更改tmp路徑的方法,一起看看吧
    2016-10-10
  • Linux系統(tǒng)中sudo命令的十個技巧總結(jié)

    Linux系統(tǒng)中sudo命令的十個技巧總結(jié)

    Linux 下使用Sudo 命令,可以讓普通用戶也能執(zhí)行一些或者全部的root命令,下面這篇文章主要給大家介紹了關(guān)于Linux系統(tǒng)中sudo命令的十個技巧,需要的朋友可以參考借鑒,下面來一起看看吧。
    2018-04-04

最新評論