Skip to content

Instantly share code, notes, and snippets.

@relkai relkai/sleeptimer.sh
Last active Sep 3, 2018

Embed
What would you like to do?
Sleep Timer
#!/bin/bash
# Sleep Timer v2.1 fuer MythTV oder andere HTPC Systeme
# 24.02.2013 // Erste Version
# 02.06.2014 // Um XBMC JSON-RPC Aufrufe erweitert
# Matthias Bodsch // bodsch.com
# -- Variablen definieren --------------------------------------------------
frontend_ip="127.0.0.1"
timer_steps=(180 150 120 90 60 45 30 15 0)
# Wie soll die Bildschirmausgabe erfolgen
# "osd_cat", "mythutil", "xbmc-json" oder "echo" (bash)
msg_cmd="xbmc-json"
# Welcher Shutdown Befehl soll verwendet werden
# "shutdown", "sudo_shutdown" oder "xbmc-json"
shutdown_cmd="xbmc-json"
# Zeichensatz fuer osd_cat
osdcat_font="-adobe-helvetica-bold-r-*-*-68-*-*-*-*-*-iso8859-1"
# tmp Datei
tmp_file="/tmp/sleeptimer.tmp"
# -- Funktionen definieren -------------------------------------------------
print_msg ()
{
if [ ${msg_cmd} == "osd_cat" ]; then
if [ ${osdcat_pid} ] && [ -f /proc/${osdcat_pid}/exe ]; then
kill ${osdcat_pid}
fi
echo "${1}" | ${msg_cmd} -s 2 -o 10 -i 10 --align=left --delay=${2} --font=${osdcat_font} & osdcat_pid=$!
elif [ ${msg_cmd} == "mythutil" ]; then
${msg_cmd} --message --bcastaddr="${frontend_ip}" --message_text="${1}" --timeout="${2}" &
elif [ ${msg_cmd} == "xbmc-json" ]; then
curl -s -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"GUI.ShowNotification\",\"params\":{\"title\":\"Sleep Timer\",\"message\":\"${1}\",\"image\":\"${3}\"}}" http://${frontend_ip}:8080/jsonrpc > /dev/null 2>&1
elif [ ${msg_cmd} == "echo" ]; then
${msg_cmd} -e "${1} \033[1A\033[2K"
else
echo "Falscher Parameter fuer 'msg_cmd'"
exit -1
fi
}
read_tmp ()
{
if [ -f ${tmp_file} ]; then
source ${tmp_file}
else
sleeptimer_helper="manual"
fi
}
kill_current ()
{
if [ ${current_pid} ] && [ -f /proc/${current_pid}/exe ]; then
kill ${current_pid} $(pgrep -P ${current_pid})
fi
}
timer_set ()
{
# Anzahl der Timer Steps sowie min und max Werte ermitteln
timer_count=${#timer_steps[@]}
max_timer=${timer_steps[0]}
min_timer=${timer_steps[$[${timer_count}-2]]}
if [ ${sleeptimer_helper} == "manual" ]; then
if [ ${sleeptimer} ]; then
# deaktivieren, wenn Timer bereits auf maximalen Wert
if [ ${sleeptimer} -eq ${max_timer} ]; then
sleeptimer=0
else
# Timer auf den naechsthoeheren Wert setzen
for ((i=1; i<${#timer_steps[@]}; i++)); do
if [ ${sleeptimer} -lt ${timer_steps[$[${i}-1]]} -a ${sleeptimer} -ge ${timer_steps[${i}]} ]; then
sleeptimer=${timer_steps[$[${i}-1]]}
fi
done
fi
else
# Timer auf den kleinsten Wert setzen, wenn das erste Mal ausgefuehrt
sleeptimer=${min_timer}
fi
fi
}
write_tmp ()
{
echo -e "sleeptimer=${1}\nsleeptimer_helper=${2}\ncurrent_pid=$$" > ${tmp_file}
}
timer_exec ()
{
min=${sleeptimer}
sek=60
if [ ${min} -eq 0 ]; then
print_msg "Sleep deaktiviert" 3 warning
rm ${tmp_file}
else
print_msg "Sleep in ${min} Minuten" 3 info
while [ ${min} -ge 2 ]
do
if [ ${min} -eq 2 ]; then
print_msg "Sleep in ${min} Minuten" 3 info
fi
sleep 60
min=$[${min}-1]
write_tmp ${min} auto
done
while [ ${sek} -ge 1 ]
do
print_msg "Sleep in ${sek} Sekunden" 1 info
sleep 1
sek=$[${sek}-1]
done
shut_down
fi
}
shut_down ()
{
print_msg "Herunterfahren des Systems" 2 error
sleep 2
rm ${tmp_file}
if [ ${shutdown_cmd} == "shutdown" ]; then
/sbin/shutdown -h now
elif [ ${shutdown_cmd} == "sudo_shutdown" ]; then
sudo /sbin/shutdown -h now
elif [ ${shutdown_cmd} == "xbmc-json" ]; then
curl -s -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1,"jsonrpc":"2.0","method":"System.Shutdown"}' http://${frontend_ip}:8080/jsonrpc > /dev/null 2>&1
else
print_msg "Falscher Parameter fuer shutdown_cmd" 3 error
exit -1
fi
}
# -- Skript ausfuehren -----------------------------------------------------
echo
# Tempfile auslesen
read_tmp
# vorigen Parent-Prozess und Child-Prozesse beenden
kill_current
# Sleeptimer festsetzen
timer_set
# Tempfile schreiben
write_tmp ${sleeptimer} manual
# Timer ausfuehren
timer_exec
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.