最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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),當(dāng)系統(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)文章

  • SSH 上傳文件及文件夾到linux服務(wù)器的方法

    SSH 上傳文件及文件夾到linux服務(wù)器的方法

    這篇文章主要介紹了SSH 上傳文件及文件夾到linux服務(wù)器的方法,需要的朋友可以參考下
    2018-07-07
  • Linux KVM的QCOW2 和 ROW的詳解及區(qū)別介紹

    Linux KVM的QCOW2 和 ROW的詳解及區(qū)別介紹

    這篇文章主要介紹了Linux QCOW2 和 ROW的詳解及區(qū)別介紹的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • linux安裝git的方法步驟

    linux安裝git的方法步驟

    這篇文章主要介紹了linux安裝git的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Linux之權(quán)限管理解讀

    Linux之權(quán)限管理解讀

    本文介紹了Linux下的超級用戶和普通用戶以及它們的命令提示符,接著,詳細解釋了權(quán)限的概念、訪問者的分類、文件類型與訪問權(quán)限以及文件權(quán)限值的表示方法,最后,文章討論了文件權(quán)限的設(shè)置方法,包括chmod、chown和chgrp命令,并列舉了一些常見權(quán)限問題
    2025-03-03
  • linux No space left on device由索引節(jié)點(inode)爆滿引發(fā)500問題

    linux No space left on device由索引節(jié)點(inode)爆滿引發(fā)500問題

    這篇文章主要介紹了linux No space left on device由索引節(jié)點(inode)爆滿引發(fā)500問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Linux上也有10個流行的Windows應(yīng)用程序

    Linux上也有10個流行的Windows應(yīng)用程序

    今天小編就為大家分享一篇關(guān)于在linux上也流行的windows程序,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-09-09
  • Linux下重新啟動Tomcat的步驟詳解

    Linux下重新啟動Tomcat的步驟詳解

    這篇文章主要給大家介紹了Linux下重新啟動Tomcat的步驟,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • 阿里云Centos7安裝LNMP+wordpress

    阿里云Centos7安裝LNMP+wordpress

    這篇文章主要介紹了阿里云Centos7安裝LNMP+wordpress的方法和步奏,十分的簡潔明了,推薦給大家,有需要的小伙伴可以參考下
    2016-04-04
  • linux查看系統(tǒng)編碼和修改系統(tǒng)編碼的方法

    linux查看系統(tǒng)編碼和修改系統(tǒng)編碼的方法

    本文為大家介紹了linux查看系統(tǒng)編碼和修改系統(tǒng)編碼的方法,這里使用的是redhat系統(tǒng),大家參考使用吧
    2014-01-01
  • 如何給Linux虛擬機連上WiFi詳解

    如何給Linux虛擬機連上WiFi詳解

    這篇文章主要介紹了如何給Linux虛擬機連上WiFi,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論

西宁市| 永登县| 开化县| 崇礼县| 临桂县| 井研县| 渭源县| 政和县| 周宁县| 卫辉市| 沂南县| 金华市| 县级市| 瑞丽市| 清苑县| 陇西县| 岑巩县| 通海县| 西华县| 蒙城县| 大兴区| 会东县| 金坛市| 金塔县| 易门县| 平和县| 青铜峡市| 吴堡县| 修武县| 秦皇岛市| 六枝特区| 南城县| 嘉义县| 东兴市| 大名县| 凌云县| 介休市| 瑞昌市| 年辖:市辖区| 西丰县| 万宁市|