如何在休眠之后或之前运行脚本

use*_*712 8 scripts hibernate

我想在休眠前运行 4 个命令,在更新后运行另外 2 个命令。是否也可以安排每个命令的时间/顺序,如果是,那么你能解释一下吗?

Dav*_*rds 8

您可以在休眠或挂起之前和之后运行命令(注意有区别;休眠是磁盘,挂起是内存)通过在/etc/pm/sleep.d以下位置创建脚本:

#!/bin/bash

case "$1" in
  hibernate)
    # put commands to run on hibernation here
    ;;
  thaw)
    # put commands to run when returning from hibernation here
    ;;
  suspend)
    # put commands to run on suspend here
    ;;
  resume) 
    # put commands to run when returning from suspension
    ;;
esac
Run Code Online (Sandbox Code Playgroud)

脚本的文件名将决定脚本与 sleep.d 中的其他脚本相比的运行顺序。在您的脚本中,您的命令将按照您在脚本中放置的任何顺序运行。

  • 随着对 systemd 的更改,此答案仅适用于 14.04 及更早版本。请参阅 [在 Ubuntu 15.04 (systemd) 中暂停后启动脚本](http://askubuntu.com/questions/661715/make-a-script-start-after-suspend-in-ubuntu-15-04-systemd)和 [man systemd-sleep](http://manpages.ubuntu.com/manpages/wily/man8/systemd-suspend.service.8.html) 用于更高版本。 (3认同)
  • 文件按字母数字顺序排列(并因此执行)。该数字不是必需的,但它提供了一种控制脚本顺序的方法。 (2认同)