os/hosts/droppie/backup-autostop.nix

35 lines
1.1 KiB
Nix

{pkgs, ...}: let
shutdownWaitMinutes = 10;
shutdownScript = pkgs.writeShellScriptBin "shutdown-wait" ''
ssh_login_active="$(${pkgs.procps}/bin/ps aux | ${pkgs.gnugrep}/bin/grep "sshd: yule" | ${pkgs.gnugrep}/bin/grep -v grep | ${pkgs.coreutils-full}/bin/wc -l)"
if [ -n "''${ssh_login_active}" ] && [ "''${ssh_login_active}" != "0" ]; then
echo "There is still an active ssh connection"
exit 0
fi
echo "WARNING: System will be shut down within the next ${builtins.toString shutdownWaitMinutes} minutes" | ${pkgs.util-linux}/bin/wall
sleep 10
shutdown -P +${builtins.toString shutdownWaitMinutes}
'';
in {
systemd.services."shutdown-after-backup" = {
enable = true;
serviceConfig = {
ExecStart = "${shutdownScript}/bin/shutdown-wait";
Type = "oneshot";
};
};
systemd.timers."shutdown-after-backup" = {
enable = true;
timerConfig = {
OnCalendar = "*-*-* 02..11:05,15,25,35,45,55:00 Etc/UTC";
};
wantedBy = ["timers.target"];
partOf = ["shutdown-after-backup.service"];
};
}