7

Is there any way to prevent temporally suspend/hibernate from a script?

I want the screen to enter in powersave mode when appropriate, but the computer doesn't stop. Caffeine doesn't allow this: it disables all or nothing.

Why I want to do this? Because sometimes I download huge amount of files from a server via FTP. These downloads can take hours to complete.

Juan Simón
  • 1,703
  • See this(http://askubuntu.com/questions/576525/can-i-prevent-ubuntu-being-suspended-while-a-download-is-in-progress) question. You can use the script provided there to supersede suspend while any upload or download in progress. Change wlan0 to eth0 or use both if its applies. – dhiya Aug 15 '15 at 18:26

2 Answers2

6

To prvent hibernation, create /var/run/do-not-hibernate:

sudo touch /var/run/do-not-hibernate

The file /usr/lib/pm-utils/sleep.d/000kernel-change makes this work. If you need to disable suspend, create a new file for that, say /etc/pm/sleep.d/000_prevent_suspend_hibernate:

#!/bin/sh
# Prevents the machine from suspending or hibernating when
# the file /var/run/do-not-hibernate-or-suspend exist
case "$1" in
  suspend|hibernate)
    [ -f /var/run/do-not-hibernate-or-suspend ] && exit 1
    ;;
esac

Make it executable:

sudo chmod +x /etc/pm/sleep.d/000_prevent_suspend_hibernate

In case you need to prevent the machine from suspending or hibernating, create a file:

sudo touch /var/run/do-not-hibernate-or-suspend

After rebooting or removing this file, suspending and hibernating works again.

Lekensteyn
  • 174,277
  • Thanks. I'll test your script with a little change. Instead of create the file /var/run/do-not-hibernate-or-suspend, I'll test the existence of a lftp process active with "pidof lftp". – Juan Simón Oct 09 '11 at 16:00
  • It doesn't work. I think these scripts aren't used. Because the pm-suspend.log shows lines like: "/usr/lib/pm-utils/sleep.d/...". I'll test moving the script to this folder: /usr/lib/pm-utils/sleep.d/ – Juan Simón Oct 10 '11 at 08:52
  • 1
    I thought that exit 1 would stop the hibernate process but it doesn't. I'm testing now with: . "${PM_FUNCTIONS}" ... pidof lftp && inhibit – Juan Simón Oct 10 '11 at 12:31
0

Simply add a file to /etc/pm/sleep.d In this file do you checks if suspend should be allowed. If not just exit with exit 1.

For example:

tmp_start=...
tmp_stop=...

if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
    logger $0: Currently RECORDING. No Suspend!
    exit 1;
fi
JPT
  • 369