每当计算机从休眠状态唤醒时,都运行python脚本 [英] run a python script everytime the computer wakes up from hibernation

查看:557
本文介绍了每当计算机从休眠状态唤醒时,都运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python上编写了一个小脚本,该脚本从控制台调用命令行以使linux机器休眠(或在更改一个单词的情况下将其自身关闭),然后在一段时间后唤醒.通过watch命令一次又一次地调用该命令.

import os
import time

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

因此,在计算机再次唤醒后20秒,将再次调用rtcwake命令.我希望每次计算机唤醒时都可以运行另一个脚本.我已经有了其他脚本,这是一个倒计时.我想这样做是为了向用户显示还剩下多少时间,直到计算机再次关闭自身,但每次计算机唤醒后也应调用第二个python脚本

对此有何想法??谢谢

解决方案

如果将内核配置为使用APM,则应该有一个/etc/apm/resume.d目录,可以在其中放置一些在系统电源状态更改时执行的脚本. >

如果您不使用APM(或者您不想使用它),请尝试/etc/pm/sleep.d/usr/lib/pm-utils/sleep.d目录.

在每种情况下,您都可以放入这样的脚本:

#!/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

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

解决方案

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

这篇关于每当计算机从休眠状态唤醒时,都运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆