EDITAR Gracias a pa4080, agregué una línea al script a continuación y ahora funciona muy bien. No entiendo exactamente cómo, bueno.
Me gustaría hacer un trabajo cron para ajustar mi brillo a diferentes horas del día. Después de hacer algunas búsquedas en Google y prueba y error, escribí el siguiente script bash que funciona bien:
#!/bin/bash
export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}')
H=$(date +%H)
if (( 00 <= 10#$H && 10#$H < 07 )); then
xrandr --output HDMI-1 --brightness .3 && xrandr --output HDMI-2 --brightness .3 && xrandr --output HDMI-3 --brightness .3
elif (( 07 <= 10#$H && 10#$H < 10 )); then
xrandr --output HDMI-1 --brightness .5 && xrandr --output HDMI-2 --brightness .5 && xrandr --output HDMI-3 --brightness .5
elif (( 10 <= 10#$H && 10#$H < 19 )); then
xrandr --output HDMI-1 --brightness .7 && xrandr --output HDMI-2 --brightness .7 && xrandr --output HDMI-3 --brightness .7
elif (( 19 <= 10#$H && 10#$H < 22 )); then
xrandr --output HDMI-1 --brightness .5 && xrandr --output HDMI-2 --brightness .5 && xrandr --output HDMI-3 --brightness .5
elif (( 22 <= 10#$H && 10#$H < 23 )); then
xrandr --output HDMI-1 --brightness .3 && xrandr --output HDMI-2 --brightness .3 && xrandr --output HDMI-3 --brightness .3
else
echo "Error"
fi
Luego usé crontab -e para agregar la siguiente línea:
0 * * * * /home/piney/screendimmer.sh
El cronjob se activa pero el script no se ejecuta. ¿Qué estoy haciendo mal?
Mejor respuesta
Cron proporciona un conjunto limitado de variables de entorno de forma predeterminada. Para obtener xrandr
para trabajar a través de un trabajo Cron, debe exportar el valor del $DISPLAY
del usuario actual variable. Para hacer eso, agregue la siguiente línea al comienzo de su secuencia de comandos (o agréguela dentro de crontab
archivo):
export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}')
Referencias:
-
Programa Crontab y C que debe ejecutarse en una ventana de terminal
-
¿Cómo encontrar mediante programación el valor actual de DISPLAY cuando DISPLAY no está configurado?
Me gustó la idea y ya la implementé en mi sistema. Esta es mi versión del script anterior:
#!/bin/bash
# While the user is not logged in == until the $DISPLAY variable is unset or empty
unset DISPLAY
while [ -z "$DISPLAY" ] || [ "$DISPLAY" == "" ]; do
DISPLAY=$(w "$(id -un)" | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}' 2>/dev/null)
if [ "$DISPLAY" == "" ]; then sleep 30; else export DISPLAY="$DISPLAY"; fi
done
brightness(){
# Get the list of the active monitors automatically
# To set this list manually use: OUT=( VGA-1 HDMI-1 HDMI-2 HDMI-3 )
OUT=$(xrandr --listactivemonitors | awk 'NR!=1{print " "$NF" "}')
# Adjust the brightness level for each monitor
for current in "${OUT[@]}"; do xrandr --output "${current// /}" --brightness "$1"; done
}
if [ -z "${1+x}" ]; then # If the scrip is called from Cron or CLI without an argument: 'brightness'
H=$(date +%-H)
if (( 0 <= "$H" && "$H" < 7 )); then brightness ".5"
elif (( 7 <= "$H" && "$H" < 10 )); then brightness ".6"
elif (( 10 <= "$H" && "$H" < 19 )); then brightness ".7"
elif (( 19 <= "$H" && "$H" < 22 )); then brightness ".6"
elif (( 22 <= "$H" && "$H" < 24 )); then brightness ".5"
else echo "Error"
fi
else brightness "$1" # If the scipt is called with an additional argument: 'brightness "<value>"'
fi
-
El script puede obtener la lista de los monitores activos automáticamente. Lo he probado con dos monitores.
-
Una buena idea es colocar el archivo ejecutable en
/usr/local/bin
, por lo que estará disponible también como comando de shell. Supongamos que se llamabrightness
. -
El script puede usar argumentos, que anularán los valores de brillo predeterminados, por ejemplo:
brightness .9
. -
Mientras
/usr/local/bin
no aparece en elcrontab
's$PATH variable
, los trabajos de Cron deben usar la ruta completa:@hourly /usr/local/bin/brightness
-
Probablemente el
@reboot
Los trabajos cron no funcionarán con la versión actual del script.