Skip to content

Instantly share code, notes, and snippets.

@jdtsmith
Created December 15, 2024 16:54
Show Gist options
  • Save jdtsmith/90ee949bcef023633f9c5127fac5e7c4 to your computer and use it in GitHub Desktop.
Save jdtsmith/90ee949bcef023633f9c5127fac5e7c4 to your computer and use it in GitHub Desktop.
Cleanup TRAMP connections prior to sleeping with Hammerspoon
local cw = require "hs.caffeinate.watcher"
function cleanup_tramp_on_sleep(ev_type)
if ev_type == cw.systemWillSleep then
hs.execute("/usr/local/bin/emacsclient -e " ..
"'(when (tramp-list-connections) " ..
" (message \"Cleaning up TRAMP before sleep\") " ..
" (tramp-cleanup-all-connections))'")
end
end
-- Note: watcher must be a global to continue running
sleep_watcher = cw.new(cleanup_tramp_on_sleep)
sleep_watcher:start()
print("Watching for sleep with ", sleep_watcher)
@jdtsmith
Copy link
Author

This helps prevent tramp lockup in emacs by cleaning up all tramp connections prior to sleep. Operating in any remote emacs buffers will automatically re-establish the connection.

@Thaodan
Copy link

Thaodan commented Dec 31, 2024

For Linux you can do the same using a function which is executed when the DBus suspend signal occurs.
Check out gnus-dbus-sleep-handler for reference.

I this context it should be a little cleaner to write the elisp function in Emacs e.g. in init.el and then call it with emacsclient e.g. as in emacsclient -e my_function. To bad you can't just subscribe to Mach IPC messages as you can with DBus in Emacs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment