2023-01-28 21:27:52 +00:00
|
|
|
{pkgs, ...}: let
|
2023-10-04 10:09:28 +00:00
|
|
|
shutdownWaitMinutes = 10;
|
2022-11-27 22:35:12 +00:00
|
|
|
shutdownScript = pkgs.writeShellScriptBin "shutdown-wait" ''
|
2023-11-08 16:09:06 +00:00
|
|
|
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)"
|
2022-11-27 20:17:33 +00:00
|
|
|
|
2023-11-06 20:50:28 +00:00
|
|
|
if [ -n "''${ssh_login_active}" ] && [ "''${ssh_login_active}" != "0" ]; then
|
|
|
|
echo "There is still an active ssh connection"
|
2023-11-08 16:09:06 +00:00
|
|
|
exit 0
|
2022-11-27 20:36:46 +00:00
|
|
|
fi
|
2022-11-27 20:17:33 +00:00
|
|
|
|
2023-11-01 15:27:29 +00:00
|
|
|
echo "WARNING: System will be shut down within the next ${builtins.toString shutdownWaitMinutes} minutes" | ${pkgs.util-linux}/bin/wall
|
2022-11-27 20:17:33 +00:00
|
|
|
|
2022-11-27 20:36:46 +00:00
|
|
|
sleep 10
|
2022-11-27 20:17:33 +00:00
|
|
|
|
2022-11-27 20:36:46 +00:00
|
|
|
shutdown -P +${builtins.toString shutdownWaitMinutes}
|
2022-11-27 20:17:33 +00:00
|
|
|
'';
|
2023-01-28 21:27:52 +00:00
|
|
|
in {
|
2022-11-27 20:17:33 +00:00
|
|
|
systemd.services."shutdown-after-backup" = {
|
|
|
|
enable = true;
|
|
|
|
serviceConfig = {
|
2022-11-27 22:35:12 +00:00
|
|
|
ExecStart = "${shutdownScript}/bin/shutdown-wait";
|
|
|
|
Type = "oneshot";
|
2022-11-27 20:17:33 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers."shutdown-after-backup" = {
|
2024-09-06 17:29:34 +00:00
|
|
|
enable = false;
|
2022-11-27 20:36:46 +00:00
|
|
|
timerConfig = {
|
2023-11-08 16:09:06 +00:00
|
|
|
OnCalendar = "*-*-* 02..11:05,15,25,35,45,55:00 Etc/UTC";
|
2022-11-27 20:17:33 +00:00
|
|
|
};
|
2023-01-28 21:27:52 +00:00
|
|
|
wantedBy = ["timers.target"];
|
|
|
|
partOf = ["shutdown-after-backup.service"];
|
2022-11-27 20:17:33 +00:00
|
|
|
};
|
|
|
|
}
|