Skip to content

Instantly share code, notes, and snippets.

@dzove855
Last active November 4, 2022 09:25
Show Gist options
  • Select an option

  • Save dzove855/bed3bacad442e3a148b583bf421a7954 to your computer and use it in GitHub Desktop.

Select an option

Save dzove855/bed3bacad442e3a148b583bf421a7954 to your computer and use it in GitHub Desktop.
#!/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