os/hosts/droppie/backup-autostop.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
983 B
Nix
Raw Normal View History

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