3

I have a python script that I need to run after resuming system from suspend. I have created an executable in /etc/pm/sleep.d/

#!/bin/bash 
case "$1" in
    hibernate|suspend|thaw|resume)
        sudo /data/fan/fanSpeedScript.sh
        ;;
esac

running sudo /data/fan/fanSpeedScript.sh does it's job in interactive terminal but the script is not running after resuming.

Here is the fanSpeedScript.sh

    #!/bin/sh
    python /data/fan/fanSpeed /data/fan/quiet

And the fanSpeed

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

Any help is appreciated.

0

These scripts works for me, perhaps someone will find these useful. There are only two scripts needed, as the script that calls the actual python script is unnecessary.

in /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

test_syslog.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())

I tested these script by running sudo pm-suspend. I also noticed that when I use a suspend applet, the script doesn't always get called. But running pm-suspend will always call the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.