▲ | Keeping SSH sessions alive with systemd-inhibit(kd8bny.com) | ||||||||||||||||||||||||||||||||||||||||
65 points by kd8bny 6 days ago | 21 comments | |||||||||||||||||||||||||||||||||||||||||
▲ | JdeBP 3 days ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||
Yes, this is a bodge. And yes, it should be unnecessary to jam in an inhibitor lock taken around the lifetime of an interactive shell permanently in the background. The author wants xyr system to not suspend either when there's someone active on the GUI or when xe is logged in over the network. Theoretically, systemd already has a whole mechanism for making this happen. The pam_systemd.so hook into PAM allows systemd-logind to track SSH sessions in its replacement for the Unix login database — all of that stuff under /run/systemd/users/, /run/systemd/seats/, and /run/systemd/sessions/. And in theory systemd-logind could respond negatively to a suspend request if there is an active SSH login session even if the GUI is idle. In practice, it does not quite have the logic for achieving this. It's close, but it does not pass around enough information for this to be done as, say, a PolicyKit rule. * https://github.com/systemd/systemd/blob/main/src/login/login... * https://github.com/systemd/systemd/blob/main/src/login/login... | |||||||||||||||||||||||||||||||||||||||||
▲ | fpoling 3 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
This has the same purpose as caffeinate command on Mac and will prevent the system from suspend while a command is running. This is useful with tmux or long compilations or tests or whatever. On Linux I rather prefer to configure my laptop to never suspend when connected to a power supply. For me the compilations and tests heavily drain battery and should not be run typically on battery. But it turned on Mac Apple really wants to suspend and configuring the same behavior is somewhat non-trivial. So in past when I used Mac i often had a terminal window running caffeinate -i sleep infinity to prevent any suspend until I hit Ctrl-C there. | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
▲ | zokier 4 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
I think this could be made into systemd (user) service fairly easily. Then you wouldn't need to worry about `disown` or tracking the PID, you'd just `systemctl --user start|stop prevent-sleep.service` If you want to be extra fancy, you could even write small program that calls the dbus api directly and then just waits to be killed. Avoids the turducken of waiting processes. | |||||||||||||||||||||||||||||||||||||||||
▲ | debugnik 3 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Something unclear about the explanation: systemd-inhibit is not running `sh & disown`, it's running `sh` and `& disown` applies to the systemd-inhibit command. If you actually daemonized the inner command, systemd-inhibit would release the lock immediately. Cool trick though! I didn't know about systemd-inhibit. | |||||||||||||||||||||||||||||||||||||||||
▲ | blueflow 4 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Of course systemd killing your SSH session is a intentional feature, not a bug. Consider disabling suspend from the logind.conf and suspend, if at all, only at explicit user request. | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
▲ | wang_li 3 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
What is the problem being solved? That your device is going to sleep or that it is interrupting SSH sessions? If it's the latter turn off keep-alives and an idle SSH session will live through sleeps and network interruptions. | |||||||||||||||||||||||||||||||||||||||||
▲ | 1970-01-01 4 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Why not fix the keep alive time? Yes, there's no wrong way to do it, but IMHO the nix philosophy states you should at least try to stick your problem and your solution together. https://www.golinuxcloud.com/keep-alive-ssh-sessions-in-linu... | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
▲ | heipei 3 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Sounds complicated. I just use autossh from the CLI and it reconnects if my laptop (or the remote machine) wakes up again. | |||||||||||||||||||||||||||||||||||||||||
▲ | 1oooqooq 4 days ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||
i think i used a laptop for a few hours in my life before I decided to simply disable all autosleep features. computer is on, it's on. lid close, screen is off. done. | |||||||||||||||||||||||||||||||||||||||||
|