run a python script everytime the computer wakes up from hibernation

半世苍凉 提交于 2019-12-21 05:39:13

问题


i wrote a small script on python that calls a command line from the console in order to hibernate a linux machine (or shut itself down in case one word is changed) and then wake up after some time. The command is called again again and again through the watch command.

import os
import time

os.system("watch -n 20 sudo rtcwake -u -s 10 -m mem")

So the rtcwake command is called again 20 seconds after the pc has wokn up again. I would like another script to be run every time the computer wakes up. I already have this other script, it is a countdown. I want to do this in order to show the user how much time is left until the computer shut itself down again, but that second python script should also be called every time after the computer wakes up

Any ideas on this?? Thank you


回答1:


If your kernel is configured to use APM, you should have a /etc/apm/resume.d directory where you could put some scripts executed whenever the system power state changes.

If you don't use APM (or if you don't want to be aware of this) try the /etc/pm/sleep.d or /usr/lib/pm-utils/sleep.d directories.

In every case, you could put in a script like this :

#!/bin/sh

case "$1" in
        resume)
                #Do what you need on resume
                ;;
        thaw)
                #Do what you need on thaw
                ;;
        suspend)
                #Do what you need on suspend
                ;;
        hibernate)
                #Do what you need on hibernate
                ;;
esac


来源:https://stackoverflow.com/questions/17852683/run-a-python-script-everytime-the-computer-wakes-up-from-hibernation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!