My custom sleep script.
Earlier I posted about a custom sleep script so that my laptop would suspend for a few hours and then automatically wake up and hibernate to disk if not woken up. I was still struggling writing it in bash, so I tried writing in python, and finally got it to work! Here's the script below. I added the command to the script in /etc/acpi/handler.sh where it responds to lid events. If it matters, releasing under GNU GPL v3.
import os
import sys
import time
import logging
def init_logger1(loggername, filename =None):
if not filename:
filename ='~/sleep.log'
logger = logging.getLogger(loggername)
logger.setLevel(logging.DEBUG)
ch = logging.FileHandler(filename)
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter
ch.setFormatter(formatter)
# add ch to logger
if len(logger.handlers)==0:
logger.addHandler(ch)
return logger
logger = init_logger1('smartsleep')
pid = str(os.getpid())
logger.info('smartsleep called, pid={0}'.format(pid))
pidfile = "/tmp/smartsleep.pid"
if os.path.isfile(pidfile):
logger.info ("{0} already exists, exiting".format(pidfile))
sys.exit()
logger.info('waiting 10 seconds...'.format(pid))
time.sleep(10)
file1 = open('/proc/acpi/button/lid/LID/state')
status = file1.read()
logger.info(status)
if 'closed' in status:
open(pidfile, 'w').write(pid)
logger.info('lid still closed, going to sleep')
os.popen("rtcwake -m mem -u -v -s 10800", 'w')
logger.info('woke up...waiting 30 seconds')
time.sleep(30)
file1 = open('/proc/acpi/button/lid/LID/state')
status = file1.read()
if 'closed' in status:
logger.info('waited 30 seconds, lid still closed so hibernating')
os.popen("pm-hibernate")
else:
logger.info('lid is open, so exiting')
os.unlink(pidfile)
sys.exit()
else:
logger.info('still open, quitting...')
About this post
- Posted
- Archived
- Score
- 34 · 100% up
- Kind
- Text
- ID
- ppfkc
18 comments
I have one in bash on my netbook. I'm at work right now, and I'll likely forget by the time I get home. Could someone reply to this as a reminder?
Update
The first I use is a script called
lid.shthat triggers when the lid is closed (look into ACPI events to do that):The
.allows2ramfile is simply a file that contains the word "no" if I have suspend-on-lid disabled (if I'm just moving to another room or another desk, I don't want the machine to go to sleep when I close it. I have quick keybinds set up to toggle whether it suspends or not). Grepping the state of the ac-adapter is because, when the device is plugged in, I generally don't want it to sleep even with the lid closed. I like to plug it in, shove it in the corner, and then SSH into it, or VNC if I really need to.lid.shcallssleep-then-hibernate.shwhich looks like:And that's it. It works flawlessly on my machine.
replying like a bauss
Updated original post, thanks for replying!
Hehe. You're sugardeath and your name is Satoshi.
I..yes.. Is there some significance? They're both aliases, actually. The only machine I use my real name as a user name is the server at work.
I'm home from work now.... you should be to.
Updated original post, thanks for replying!
Thanks for sharing! I'm trying to teach myself how to write system scripts for various things and it really helps to see people's solutions to their problems. Helps give me ideas of stuff to try!
It is amazing to ponder what is possible, although I did most of this kind of script writing when setting up my arch system and have mostly been lazy since then (a year and a half).
I feel like a solution in search of a problem! At this point I love it when a pacman -Syu breaks something, my only chance to learn.
pm-utils has a command pm-suspend-hybrid, which leaves computer in sleep mode until a certain battery level gets low, at which point it hibernates. Haven't tested it out as I haven't bothered to get hibernate working, but should work fine.
As an exercise, I tried rewriting it in bash, though I don't know if it works or not since I've got trouble with hibernating as it is.
NOTE: I used "logger" for this, so all messages show up in syslog. You can also use the "-p" option to specify priority and facility (see man logger) or just use "echo $MSG >> $LOGFILE".
I just tested it. It appears to work!
Caveat emptor though, I'm not really sure about the pid-handling part.
For one, it should break if the filename starts with a dash, though inserting "--" after "pidof" should take care of that.
If you have any questions regarding that script (or bash in general), I'm available daily under this username.