My PC is set up to save power quite aggressively, by suspending after only 5 minutes of inactivity. This is fine most of the time, when the PC is periodically used by the family to quickly read email or check into facebook. But this causes me problems when working from home.
I use OpenVPN to connect to the office and run screen sessions and the occasional X11-forwarded GUI, and the suspend operation causes OpenVPN to be disconnected once I resume. This means that every time I have a coffee break, or try to whiteboard something, I get back to the computer and have to re-establish the ssh session, resume the screen session and restart any X11 apps I might have been using. It's at this point that I remember to change the power saving options to disable the suspend, but then I forget to re-enable it once I've finished working for the day.
To fix this without me having to remember to do anything special, we can automatically prevent the suspend (or hibernation) of the PC when the
openvpn client is running.
As root, create a new file called
/usr/lib/pm-utils/sleep.d/000vpn-disable-sleep, give it the contents shown below and make it executable.
sudo vim /usr/lib/pm-utils/sleep.d/000vpn-disable-sleep
#!/bin/sh # Prevent machine from suspending or hibernating when openvpn is running. case "$1" in suspend|hibernate) /bin/pidof openvpn && exit 1 ;; esac exit 0
sudo chmod +x /usr/lib/pm-utils/sleep.d/000vpn-disable-sleep
Works for me on Ubuntu 14.04 LTS. YMMV.