Hibernation is a great feature on any computer (be it laptop especially, workstation or even server). It allows for a temporary pause in execution in which the system in question can be unplugged, cleaned, modified, or moved.
Hibernation is even handy-er on power failures, where the UPS provides a grace period of the system to go to sleep, only to wake up once the power resumes.
But what happens when some services don’t play nice and fail to resume properly from sleep? What happens when these services run on an unmonitored server, and those services are needed to always run (except when the power goes out)?
Fortunately, Linux’s power management utilities provide the necessary functionality to control services/processes/actions depending on the power state of the system.
Create the following file and make it executable by everyone:
touch /etc/pm/sleep.d/00_powerhandler
chmod 755 /etc/pm/sleep.d/00_powerhandler
Then paste the following content in it and adjust to your needs:
#!/bin/bash # script to gracefully stop services on hibernation and restart them on wakeup # le: 2014.07.11 / zedt.eu LOG="/var/log/powerhandler.log" . /usr/lib64/pm-utils/functions # for 32bit OS change to lib/ case $1 in hibernate|suspend) # this is where things that happen when powering down go echo "$(date): suspending to RAM..." >>$LOG /sbin/service motion stop /usr/bin/killall myscript ;; # make sure the last command in each case block ends with ;; thaw|resume) # and things that should happen after wakeup echo "$(date): resume from suspend..." >>$lOG sleep 30 /sbin/service motion start nohup /prg/myscript & ;; *) echo "$(date): unhandled state; nothing to do" >>$LOG ;; esac
You can check the /var/log/powerhandler.log logfile after a hibernation/wake session to monitor things out.