diff options
author | Simon Rettberg | 2017-04-11 15:16:00 +0200 |
---|---|---|
committer | Simon Rettberg | 2017-04-11 15:16:00 +0200 |
commit | 962aabf178c1833d60d614706cacff057ff4bbc5 (patch) | |
tree | d2285a52bdbf157d17e3f8b8595d705957027bdb /core/modules/idleaction/data | |
parent | [ssh-auth-keys] Try to filter invalid data from authkeys.d, don't break on no... (diff) | |
download | mltk-962aabf178c1833d60d614706cacff057ff4bbc5.tar.gz mltk-962aabf178c1833d60d614706cacff057ff4bbc5.tar.xz mltk-962aabf178c1833d60d614706cacff057ff4bbc5.zip |
[idleaction] Support detached and delayed operation
Diffstat (limited to 'core/modules/idleaction/data')
-rwxr-xr-x | core/modules/idleaction/data/opt/openslx/scripts/idleaction-scheduled_action | 151 |
1 files changed, 94 insertions, 57 deletions
diff --git a/core/modules/idleaction/data/opt/openslx/scripts/idleaction-scheduled_action b/core/modules/idleaction/data/opt/openslx/scripts/idleaction-scheduled_action index 6ef6969c..5c5f2a06 100755 --- a/core/modules/idleaction/data/opt/openslx/scripts/idleaction-scheduled_action +++ b/core/modules/idleaction/data/opt/openslx/scripts/idleaction-scheduled_action @@ -5,73 +5,110 @@ if [ "$(whoami)" != "root" ]; then exit 1 fi -if [ $# -ne 1 ]; then - echo "$0 <reboot|poweroff>" - exit 1 +DETACH= +if [ "$1" = "--detach" ]; then + DETACH=yes + shift +fi + +if [ $# -lt 1 ]; then + echo "$0 [--detach] <reboot|poweroff> [delay_minutes]" + exit 2 fi MODE=$1 +DELAY=$2 if [ "$MODE" != "reboot" ] && [ "$MODE" != "poweroff" ]; then slxlog "idleaction-failed-call" "Invalid call to idleaction-scheduled_action. Mode '$MODE' unknown." - exit 1 + exit 3 fi NUM=$(who | wc -l) -[ "$NUM" = "0" ] && $MODE # Easy way out - machine is idle - -# Someone logged in -for MINUTES in 5 X X X X X X X 3 X X X 2 X X X 1 X X X; do - NUM=$(who | wc -l) - [ "$NUM" = "0" ] && break - if [ "$MINUTES" != "X" ]; then - USERS=0 - if [ "$MODE" = "reboot" ]; then - MESSAGE="Das System wird in $MINUTES Minute(n) neugestartet, bitte beenden Sie Ihre Sitzung. -The system will reboot in $MINUTES minute(s). Please save your work and end the session." - else - MESSAGE="Dieser Rechner wird in $MINUTES Minute(n) abgeschaltet, bitte beenden Sie Ihre Sitzung. -The system will power off in $MINUTES minute(s). Please save your work and end the session." - fi - for d in $(who | awk '{print $2}' | sort -u); do - if [ "${d:0:1}" = ":" ]; then - # X11 - export DISPLAY=$d - export XAUTHORITY=$(ps a | grep " $DISPLAY " | grep -o -- '-auth.*$' | grep -m1 -v grep | awk '{print $2}') - # TODO: Don't look... We need to pick a proper util for this - /opt/openslx/cups/printergui --error "$MESSAGE" & - unset DISPLAY XAUTHORITY - USERS=$(( $USERS + 1 )) - elif [ "${d:0:3}" = "tty" ]; then - # Regular tty - cat > "/dev/$d" <<-EOF - * - *************************** - $MESSAGE - *************************** - * - EOF - USERS=$(( $USERS + 1 )) - elif [ "${d:0:3}" = "pts" ]; then - # pts - xterm or ssh session - cat > "/dev/pts/${d:3}" <<-EOF - * - *************************** - $MESSAGE - *************************** - * - EOF - USERS=$(( $USERS + 1 )) - fi - done - FILE=$(mktemp) - who > "$FILE" - slxlog "idleaction-busy" "Postponed $MODE ($MINUTES minutes max.) - $USERS user(s) still active" +if [ -z "$DELAY" ] && [ "$NUM" = "0" ]; then + # Easy way out - machine is idle and no delay + ( + # Detach, close all in/out fds - needed for some ssh clients to rpevent them from hanging + exec < /dev/null &> /dev/null sleep 1 - rm -- "$FILE" + $MODE + ) & + disown + exit 0 +fi + +runaction () { + # Someone logged in or delay requested + [ -n "$DETACH" ] && sleep 1 + if [ -n "$DELAY" ] && [ "$DELAY" -gt 0 ]; then + echo "Doing $MODE in $DELAY minutes!" + sleep $(( DELAY * 60 )) fi - sleep 15 -done + for MINUTES in 5 X X X X X X X 3 X X X 2 X X X 1 X X X; do + NUM=$(who | wc -l) + [ "$NUM" = "0" ] && break + if [ "$MINUTES" != "X" ]; then + USERS=0 + if [ "$MODE" = "reboot" ]; then + MESSAGE="Das System wird in $MINUTES Minute(n) neugestartet, bitte beenden Sie Ihre Sitzung. + The system will reboot in $MINUTES minute(s). Please save your work and end the session." + else + MESSAGE="Dieser Rechner wird in $MINUTES Minute(n) abgeschaltet, bitte beenden Sie Ihre Sitzung. + The system will power off in $MINUTES minute(s). Please save your work and end the session." + fi + for d in $(who | awk '{print $2}' | sort -u); do + if [ "${d:0:1}" = ":" ]; then + # X11 + export DISPLAY=$d + export XAUTHORITY=$(ps a | grep " $DISPLAY " | grep -o -- '-auth.*$' | grep -m1 -v grep | awk '{print $2}') + # TODO: Don't look... We need to pick a proper util for this + /opt/openslx/cups/printergui --error "$MESSAGE" & + unset DISPLAY XAUTHORITY + USERS=$(( $USERS + 1 )) + elif [ "${d:0:3}" = "tty" ]; then + # Regular tty + cat > "/dev/$d" <<-EOF + * + *************************** + $MESSAGE + *************************** + * + EOF + USERS=$(( $USERS + 1 )) + elif [ "${d:0:3}" = "pts" ]; then + # pts - xterm or ssh session + cat > "/dev/pts/${d:3}" <<-EOF + * + *************************** + $MESSAGE + *************************** + * + EOF + USERS=$(( $USERS + 1 )) + fi + done + FILE=$(mktemp) + who > "$FILE" + slxlog "idleaction-busy" "Postponed $MODE ($MINUTES minutes max.) - $USERS user(s) still active" + sleep 1 + rm -- "$FILE" + fi + sleep 15 + done + + $MODE +} + +if [ -n "$DETACH" ]; then + ( + # Detach, see above + exec < /dev/null &> /dev/null + runaction + ) & + disown + exit 0 +fi -$MODE +runaction +exit 0 |