run a python script everytime the computer wakes u

2019-03-31 05:51发布

问题:

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