Thanks @Greg for your answer, I took the liberty of expanding on it to make it more "freedesktop" compliant, since pm-utils is a freedesktop.org project.
This script now will work in config.d
, power.d
, and sleep.d
with a copy or a symlink. It'll then look for scripts to run in ~/.config/pm/
using the same folder names (config.d
, power.d
, and sleep.d
) as /etc/pm
.
#!/bin/sh
scriptdir=`cd $(dirname $0) && pwd`
USER_PM_DIR=".config/pm/$(basename $scriptdir)"
# foreach logged in user
for user in `users | grep -o "\S*" | sort -u`; do
user_home=`getent passwd "${user}" | awk -F: '{print $6}'`
# check user has a valid home-directory
[ -d $user_home ] || continue
user_pm_dir="$user_home/$USER_PM_DIR"
# check for user-pm directory
[ -d "$user_pm_dir" ] || continue
# call run-parts as $user
case "$1" in
hibernate|suspend)
su -c "run-parts --arg=\"$1\" \"${user_pm_dir}\"" "${user}"
;;
thaw|resume)
su -c "run-parts --reverse --arg=\"$1\" \"${user_pm_dir}\"" "${user}"
;;
*) exit $NA ;;
esac
done
Then this is my script to remove all keys from the ssh-agent
on suspend/hibernate, and re-add a key used for SparkleShare. Just to be sure, it also checks for other ssh-agent
s and removes all the keys from them.
#!/bin/sh
case "$1" in
hibernate|suspend)
if [ ! -z $SSH_AUTH_SOCK ] && [ -r $SSH_AUTH_SOCK ]; then
ssh-add -D
fi
for SSH_AUTH_SOCK in `find /tmp/ssh-*/agent.* -user $USER`; do
SSH_AUTH_SOCK=$SSH_AUTH_SOCK /usr/bin/ssh-add -D
done
;;
thaw|resume)
if [ ! -z $SSH_AUTH_SOCK ] && [ -r $SSH_AUTH_SOCK ]; then
/usr/bin/ssh-add -t 0 ~/.config/sparkleshare/2011-03-21_14h15.key
fi
for SSH_AUTH_SOCK in `find /tmp/ssh-*/agent.* -user $USER`; do
SSH_AUTH_SOCK=$SSH_AUTH_SOCK ssh-add -t 0 ~/.config/sparkleshare/2011-03-21_14h15.key
done
;;
esac