我有一个 python 脚本,需要在系统从挂起状态恢复后运行。我在 /etc/pm/sleep.d/ 中创建了一个可执行文件
#!/bin/bash
case "$1" in
hibernate|suspend|thaw|resume)
sudo /data/fan/fanSpeedScript.sh
;;
esac
运行sudo /data/fan/fanSpeedScript.sh
在交互式终端中完成其工作,但恢复后脚本没有运行。
这是 fanSpeedScript.sh
#!/bin/sh
python /data/fan/fanSpeed /data/fan/quiet
和风扇转速
#!/usr/bin/env python
import os
import sys
EC_IO_FILE="/sys/kernel/debug/ec/ec0/io"
if not os.path.exists(EC_IO_FILE):
os.system("modprobe ec_sys write_support=1")
def ec_write(addr,value):
with open(EC_IO_FILE,"rb") as f:
f.seek(addr)
old_value=ord(f.read(1))
if (value != old_value):
print(" %3d => %3d" % (old_value, value))
with open(EC_IO_FILE,"wb") as f:
f.seek(addr)
f.write(bytearray([value]))
else:
print(" = %3d" % value)
for line in open(sys.argv[1]).readlines():
print(line.strip())
if line.startswith(">WEC "):
addr,value=line.split()[1:3]
ec_write(int(addr,0), int(value,0))
任何帮助都将受到赞赏。
答案1
这些脚本对我来说很有用,也许有人会发现它们很有用。只需要两个脚本,因为调用实际的 python 脚本的脚本是不必要的。
在 /etc/pm/sleep.d/99_run_python.sh 中:
#!/bin/sh
# ensure that we have a working path
PATH=/sbin:/usr/sbin/:/bin:/usr/bin
# see which python is installed
if [ -x /usr/bin/python3 ]; then
PYTHON=python3
else
PYTHON=python
fi
case "${1}" in
hibernate|suspend|thaw|resume)
# call python to execute script, and pass some args to script
$PYTHON /home/some_user_name/test/test_syslog.py /data/fan/quiet/$1/
;;
esac
测试系统日志.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import logging
import logging.handlers
def main():
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler()
formatter = logging.Formatter('%(module)s.%(funcName)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("Hello from python!")
if len(sys.argv) == 2:
logger.debug("got arg = {}".format(sys.argv[1]))
else:
logger.debug("got no args")
logger.warning("Will now exit.")
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
我通过运行测试了这些脚本sudo pm-suspend
。我还注意到,当我使用暂停小程序时,脚本并不总是被调用。但运行 pm-suspend 将始终调用该脚本。