Last active
November 4, 2022 09:25
-
-
Save dzove855/bed3bacad442e3a148b583bf421a7954 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/bin/bash | |
| shopt -s extglob | |
| # requires bash >=4.4 | |
| printf -v startTime '%(%H:%M)T' | |
| printf -v startTimeTs '%(%s)T' | |
| time="${1//[hms]}" | |
| timeval="${1//[!hms]}" | |
| dowarn=0 | |
| # convert always to seconds | |
| case "$timeval" in | |
| h) | |
| ((time=time * 3600)) | |
| ;; | |
| m) | |
| ((time=time * 60)) | |
| ;; | |
| esac | |
| if (( time > 300 )); then | |
| dowarn=1 | |
| ((sleepTime=time - 300)) | |
| else | |
| sleepTime="$time" | |
| fi | |
| printf 'powernap started at %s\n' "$startTime" | |
| printf 'Your pc will poweroff at %s\n' "$( printf '%(%H:%M)T' "$((startTimeTs + time))" )" | |
| sleep "$sleepTime" | |
| if (( dowarn )); then | |
| notify-send --app-name=powernap --expire-time=300000 --urgency=critical "PC will shut down in 5m" | |
| echo "PC will shut down in 5m" | |
| echo sleep 5m | |
| fi | |
| poweroff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment