-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#12] Adding scheduled shutdown service
This commit adds support for a new global directive "ShutdownTime", like ContestTimes or ConfigExpirationTime, that schedules a shutdown at the time defined. This aproach instead of calculating when to shutdown, checks every minute if the system is ready to shutdown, checks including - Is clock in sync - Is there a scheduled time to shutdown - Is time to shutdown - Is system in always mode (I think it would not be expected / allowed to shutdown the system while in contest / event mode, right?)
- Loading branch information
Showing
5 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
base-system/usrroot/usr/lib/systemd/system/hypnotizer.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[Unit] | ||
Description=Service to check if is time to shutdown the machine | ||
|
||
[Service] | ||
ExecStart=/usr/sbin/hypnotizer | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
13 changes: 13 additions & 0 deletions
13
base-system/usrroot/usr/lib/systemd/system/hypnotizer.timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Unit] | ||
Description=TImer to check if is time to shutdown the machine | ||
|
||
|
||
[Timer] | ||
AccuracySec=1s | ||
OnUnitInactiveSec=60s | ||
OnBootSec=1s | ||
OnClockChange=false | ||
OnTimezoneChange=false | ||
|
||
[Install] | ||
WantedBy=timers.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/bash | ||
# hypnotizer.sh | ||
# This script tries to shutdown the instance | ||
# at the time specified in the global section | ||
# if the required conditions (time and mode) are met. | ||
# | ||
# Copyright (C) 2022, huronOS Project: | ||
# <http://huronos.org> | ||
# | ||
# Licensed under the GNU GPL Version 2 | ||
# <http://www.gnu.org/licenses/gpl-2.0.html> | ||
# | ||
# Authors: | ||
# Daniel Cerna <dcerna@huronos.org> | ||
set -e | ||
|
||
. /usr/lib/hsync/libhlog.so | ||
|
||
## Check if clock is in sync, otherwise the current time is untrusted | ||
STATE_IS_CLOCK_SYNC=$(timedatectl show | grep NTPSynchronized | cut -d= -f2) | ||
if [ "$STATE_IS_CLOCK_SYNC" = "no" ]; then | ||
log "System time is not in sync, exiting" | ||
exit 0 | ||
fi | ||
|
||
## Getting the shutdown time | ||
SHUTDOWN_TIME="$(hos-dvar --variable-name ShutdownTime --section Global)" | ||
if [ -z "$SHUTDOWN_TIME" ] || [ "$SHUTDOWN_TIME" = "none" ]; then | ||
log "No shutdown scheduled, exiting." | ||
exit 0 | ||
fi | ||
|
||
## Getting the remaining time to shutdown | ||
SHUTDOWN_TIME_EPOCH=$(date -d "$SHUTDOWN_TIME" +%s) | ||
CURRENT_TIME_EPOCH=$(date +%s) | ||
SECONDS_TO_SHUTDOWN=$((SHUTDOWN_TIME_EPOCH - CURRENT_TIME_EPOCH)) | ||
## If secondsToShutdown > 0: don't shutdown yet | ||
## If secondsToShutdown < -300: don't shutdown (outside the 5-minute window) | ||
## If -300 <= secondsToShutdown <= 0: try to shutdown | ||
if [ "$SECONDS_TO_SHUTDOWN" -gt 0 ]; then | ||
log "Scheduled shutdown scheduled for later, exiting" | ||
exit 0 | ||
elif [ "$SECONDS_TO_SHUTDOWN" -lt -300 ]; then | ||
log "Scheduled shutdown time has exceeded the 5-minute threshold, exiting" | ||
exit 0 | ||
else | ||
log "Scheduled shutdown time has been reached and is within the 5-minutes shutdown window, proceeding" | ||
fi | ||
|
||
## If we're not in always mode, exit | ||
## We don't want to ruin neither an event or a contest | ||
if [[ $(hos-dmode) != "Always" ]]; then | ||
log "The system will shut down as soon as it goes to Always mode, exiting" | ||
exit 0 | ||
fi | ||
|
||
# If we hit this point, is totally safe to shutdown | ||
log "The stars aligned, shutting down the system" | ||
shutdown -h now |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters