I have a server running on AWS that I’d like to automatically shut down after it has been idle for some time. For me idle = no active SSH sessions. Following power management - Automatic shutdown after no activity - Unix & Linux Stack Exchange, I tried adding
services.logind.extraConfig = ''
IdleAction=poweroff
IdleActionSec=5min
'';
to my /etc/nixos/configuration.nix
, rebooted, and logged out all SSH sessions. But this does not seem to have any effect.
Is there any way to get NixOS to power off after some configured idle timeout?
samuela
2
rnhmjoj
3
How about something like this?
{
systemd.services.shutdown = {
description = "automatic shutdown for inactivity";
path = with pkgs; [ procps gawk ];
script = ''
# shutdown when no one is logged in
logged=$(who | wc -l)
if test "$logged" -lt 1 && ! pgrep -f '(sftp|tmux|something-interactive)'
then shutdown +5 'Shutting down for inactivity.'
fi
'';
};
systemd.timers.shutdown = {
wantedBy = [ "timers.target" ];
timerConfig.OnUnitActiveSec = "1h";
timerConfig.OnBootSec = "1h";
};
}
samuela
4
Yeah, that also seems like a decent solution!