source: client/shared/bin/poweroffconf @ 395c326

Last change on this file since 395c326 was 992ebb99, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

#1041 Fix poweroffconf script

poweroffconf is checking if the system is idle by doing a pgrep of
processes starting with "/opt/opengnsys/" and ignoring the ones
containing $OGETC or "poweroffconf".

ogclient process always falls under this process lookup thus making
the system seem as always active.

Ignore the ogclient process when checking if the system is not idle.

  • Property mode set to 100755
File size: 3.4 KB
RevLine 
[b608176]1#!/bin/bash
2#/**
3#@file     poweroffconf
4#@brief    Control de parada tras tiempo de inactividad para ahorro de energía.
[beed37b]5#@license  GNU GPLv3+
[ac0edb0]6#@param    int_minutos    Minutos de inactividad (opcional); "no" para deshabilitar..
[b608176]7#@note     La comprobación periódica debe ejecutarse en el "cron" del sistema.
8#@note     Fichero de configuración: /etc/poweroff.conf
9#@author   Ramón Gómez - Univ. Sevilla
10#@date     2011-10-25
[ac0edb0]11#@version  1.0.5: incluir opción para deshabilitar ahorro de energía.
12#@author   Ramón Gómez - Univ. Sevilla
13#@date     2014-02-07
[beed37b]14#@version  1.1.1: Corregir problema al cambiar de día
15#@author   Ramón Gómez - Univ. Sevilla
16#@date     2018-07-04
[b608176]17#*/
18
19
20# Variables generales.
[5bfead0]21OPENGNSYS=${OPENGNSYS:-/opt/opengnsys}  # Instalación de OpenGnsys
22OGETC=${OGETC:-$OPENGNSYS/etc}          # Configuración de OpenGnsys
[b608176]23POWEROFFCONF=/etc/poweroff.conf         # Configuración del script
[992ebb99]24OGCLIENT=ogclient
[ac0edb0]25
26# Error si no existe el fichero de configuración de ahorro de energía.
27if [ ! -f $POWEROFFCONF ]; then
28    ogRaiseError $OG_ERR_NOTFOUND "$POWEROFFCONF"
29    exit $?
30fi
31# Obtener parámetros de configuración de ahorro de energía.
[b608176]32source $POWEROFFCONF
[d4bff1a]33export TZ
[b608176]34
35case $# in
36    0)  # Sin parámetros, comprobar que existe la variable POWEROFFSLEEP.
37        if [ -z "$POWEROFFSLEEP" ]; then
38             ogRaiseError $OG_ERR_FORMAT "Sin tiempo de espera."
39             exit $?
40        fi
41        ;;
[ac0edb0]42    1)  # Nuevo timepo de espera.
43        POWEROFFSLEEP="$1"
44        # Se deshabilita si se introduce la cadena "no" como tiempo de espera.
45        [ "$POWEROFFSLEEP" == "no" ] && POWEROFFSLEEP=
46        # Error si tiempo de espera no es nulo o entero positivo.
47        if [[ ! "$POWEROFFSLEEP" =~ ^[0-9]*$ ]]; then
48            ogRaiseError $OG_ERR_FORMAT "Parámetro debe ser núm. minutos o \"no\" para deshabilitar."
49            exit $?
50        fi
51        # Actualizar fichero de configuración con nuevo periodo de parada.
[b608176]52        perl -pi -e "s/POWEROFFSLEEP=.*/POWEROFFSLEEP=$POWEROFFSLEEP/" $POWEROFFCONF
[ac0edb0]53        # Si se necesita, recalcular tiempo de parada.
54        if [ -n "POWEROFFTIME" ]; then
55            # Asignar tiempo de apagado si no está deshabilitado y actualizar fichero.
[beed37b]56            POWEROFFTIME=${POWEROFFSLEEP:+$(date --date="$POWEROFFSLEEP min" +"%s")}
[b608176]57            perl -pi -e "s/POWEROFFTIME=.*/POWEROFFTIME=$POWEROFFTIME/" $POWEROFFCONF
[ac0edb0]58        fi
59        exit 0 ;;
[b608176]60    *)  # Error de formato de ejecución.
[ac0edb0]61        ogRaiseError $OG_ERR_FORMAT "Formato: $0 [int_minutos | no]"
[b608176]62        exit $? ;;
63esac
[d4bff1a]64# Comprobar si hay algún script en ejecución (verificando compatibilidad de "pgrep").
65[ -n "$(pgrep -fa 2>&1 | grep "invalid")" ] && PGREP="pgrep -fl" || PGREP="pgrep -fa"
[992ebb99]66if [ -n "$($PGREP $OPENGNSYS | egrep -v "$OGETC|$0|$OGCLIENT")" ]; then
[b608176]67    # Eliminar tiempo de inicio de espera, si se está ejecutando operación.
68    perl -pi -e 's/POWEROFFTIME=.*$/POWEROFFTIME=/' $POWEROFFCONF
69else
70    # Si el sistema está en estado de espera, ...
[beed37b]71    NOW=$(date +"%s")
[b608176]72    if [ -z "$POWEROFFTIME" ]; then
73        # Asignar tiempo de inicio, si no estaba definido.
[beed37b]74        POWEROFFTIME=$(date --date="$POWEROFFSLEEP min" +"%s")
[b608176]75        perl -pi -e "s/POWEROFFTIME=.*$/POWEROFFTIME=$POWEROFFTIME/" $POWEROFFCONF
76    else
77        # Apagar el equipo si se sobrepasa el periodo de espera.
78        if [ $NOW -ge $POWEROFFTIME ]; then
79            $OPENGNSYS/scripts/poweroff
80        fi
81    fi
82fi
83
Note: See TracBrowser for help on using the repository browser.