Execute script before/after suspend to ram on GNU/Linux (Gentoo)


15-12-2015 13:26 by nado

Hi,

I had this annoying bug when suspending on ram my laptop, if openvpn had an instance running, it didn’t restart after a resume but rather kept its previous state. Which means, an ip address and route for a tunneling config. Unfortunately, since dhcp most likely had to reacquire an ip, I dont have any internet access anymore.

I could’nt stop properly openvpn after resuming :

sudo service openvpn.myinstance stop
[Output to paste]

Since I dont really want to do that every time i forget to stop openvpn, and better, I’d like it to be stopped automatically when suspending, one less action to do for my lazy person. pm-suspend has a simple dir hierarchy :

$ tree /etc/pm 
/etc/pm
├── config.d
│   ├── gentoo
│   ├── laptop-mode-tools
│   └── modules
├── last_known_working.quirkdb
├── power.d
└── sleep.d
    └── stop_openvpn.sh

What is interesting here in this case are the directories power.d and sleep.d, I suppose you have already understood, all executable files in /etc/pm/power.d/ will be executed when awaking [need to check if its for both boot and resume], while all files in /etc/pm/sleep.d/ will be executed when suspending to ram.

Here is my script to stop openvpn on Gentoo :

#!/bin/sh
find /etc/init.d/openvpn* -type l -exec '{}' stop \;

Note : on Gentoo, multiple openvpn instances are managed by creating a symlink on /etc/init.d/openvpn with the name of the symlink reflecting the name of the config file. Imagine you have a config file /etc/openvpn/myserver.conf, the init service for this config would be /etc/init.d/openvpn.myserver.