如何设置从睡眠中唤醒的cronjob?

How to set cronjob on wake up from sleep?

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

@reboot ./do_sth

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

这不是 cron 可以管理的东西,但它可以由电源管理实用程序 (pm-utils) 管理。在阅读man pm-action时,你发现:

/etc/pm/sleep.d, /usr/lib/pm-utils/sleep.d: Programs in these directories (called hooks) are combined and executed in C sort order before suspend and hibernate with as argument suspend or hibernate. Afterwards, they are called in reverse order with argument resume and thaw respectively. If both directories contain a similar named file, the one in /etc/pm/sleep.d will get preference. It is possible to disable a hook in the distribution directory by putting a non-executable file in /etc/pm/sleep.d, or by adding it to the HOOK_BLACKLIST configuration variable.

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

#!/usr/bin/env bash
action=""

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=""
case "$action" in
   suspend|hibernate) stuff ;;
   resume|thaw)       stuff ;;
esac