Tech News
← Back to articles

Keeping SSH sessions alive with systemd-inhibit

read original related products more articles

In my Home Lab network I use my desktop for most of my development. Since it’s a desktop and not a server it abides by the power policy setting I have selected. This works great unless I’m working a remote session from anything else such as my laptop, cell, etc.

My usual workflow is just an ssh session with tmux, where I wake up the system by issuing a Wake on LAN (wol) from an always on server. After the typical timeout around 30 mins the system goes to sleep which drops the session and forces me to send another wol and reconnect via ssh and then reattach to tmux.

annoying eh? So here is what I came up with.

So what#

On systemd enabled systems the blocking of suspend/sleep actions can be controlled by systemd-inhibit . Usually when you block sleep from the desktop environment it’s using this same mechanism under the hood. So, what does it do?

Let us rtfm systemd-inhibit - Execute a program with an inhibition lock taken systemd-inhibit [OPTIONS…] [COMMAND] [ARGUMENTS…] systemd-inhibit may be used to execute a program with a shutdown, sleep, or idle inhibitor lock taken. The lock will be acquired before the specified command line is executed and released afterwards. …

So basically the inhibit action will block sleep until the chosen executable exits. Although useful, I find this to be cumbersome thinking ahead; what do I want to run, should it be blocking, or is it long-running enough for what I want to do. I need something more flexible.

Let’s make it happen#

The Requirements: We need a long-running process that never exits, can’t suck pointless resources, and needs to be easy to kill. So lets slap a few things together and see how they work.

systemd-inhibit --no-ask-password --what = idle --who = "me" --why = "cuz I said so" sh & disown ; export INHIBIT_PID = $!

... continue reading