【问题标题】:How to set cronjob on wake up from sleep? [closed]如何在从睡眠中醒来时设置 cronjob? [关闭]
【发布时间】:2019-10-16 03:44:56
【问题描述】:

例如,如果您希望在每次重新启动后运行 cron 作业,您可以将这样的内容添加到您的 cron 文件中:

@reboot ./do_sth

有没有类似于从睡眠状态中醒来的东西?

【问题讨论】:

    标签: linux cron power-management crontrigger


    【解决方案1】:

    这不是 cron 可以管理的,但可以通过 Power Management Utilities (pm-utils) 管理。在阅读man pm-action时,你会发现:

    /etc/pm/sleep.d, /usr/lib/pm-utils/sleep.d: 这些目录中的程序(称为挂钩)在挂起和休眠之前以 C 排序顺序组合并执行,参数为suspendhibernate。之后,它们分别以参数resumethaw 以相反的顺序调用。如果两个目录都包含类似的命名文件,则/etc/pm/sleep.d 中的文件将优先。可以禁用挂钩 通过将非可执行文件放入/etc/pm/sleep.d 或将其添加到HOOK_BLACKLIST 配置变量中来分发目录。

    所以您需要做的就是在/etc/pm/sleep.d 中创建一个如下所示的脚本:

    #!/usr/bin/env bash
    action="$1"
    
    case "$action" in
       suspend)
            # List programs to run before, the system suspends
            # to ram; some folks call this "sleep"
       ;;
       resume)
            # List of programs to when the systems "resumes"
            # after being suspended
       ;;
       hibernate)
            # List of programs to run before the system hibernates
            # to disk; includes power-off, looks like shutdown
       ;;
       thaw)
            # List of programs to run when the system wakes
            # up from hibernation
       ;;
    esac
    

    显然,如果您不想区分 suspendhibernate,或者 resumethaw,可以将其更改为:

    #!/usr/bin/env bash
    action="$1"
    case "$action" in
       suspend|hibernate) stuff ;;
       resume|thaw)       stuff ;;
    esac
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多