MODIFICA Grazie a pa4080 ho aggiunto una riga allo script qui sotto e ora funziona alla grande. Non capisco esattamente come, vabbè.
Vorrei fare un lavoro cron per regolare la mia luminosità in diverse ore del giorno. Dopo aver cercato su Google e tentativi ed errori, ho scritto il seguente script bash che funziona bene:
#!/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
Quindi ho usato crontab -e per aggiungere la seguente riga:
0 * * * * /home/piney/screendimmer.sh
Il cronjob viene attivato ma lo script non viene eseguito. Cosa sbaglio?
Migliore risposta
Cron fornisce un set limitato di variabili di ambiente per impostazione predefinita. Per ottenere xrandr
per eseguire un processo Cron, devi esportare il valore del $DISPLAY
dell'utente corrente variabile. Per farlo aggiungi la riga seguente all'inizio del tuo script (o aggiungilo all'interno di crontab
file):
export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}')
Riferimenti:
-
Crontab e programma C che dovrebbe essere eseguito in una finestra di terminale
-
Come trovare a livello di codice il valore corrente di DISPLAY quando DISPLAY non è impostato?
L'idea mi è piaciuta e l'ho già implementata nel mio sistema. Ecco la mia versione dello script precedente:
#!/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
-
Lo script è in grado di ottenere automaticamente l'elenco dei monitor attivi. L'ho testato con due monitor.
-
Una buona idea è posizionare il file eseguibile in
/usr/local/bin
, quindi sarà disponibile anche come comando di shell. Supponiamo che si chiamibrightness
. -
Lo script è in grado di utilizzare un argomento, che sovrascriverà i valori di luminosità predefiniti, ad esempio:
brightness .9
. -
Mentre
/usr/local/bin
non è elencato nelcrontab
's$PATH variable
, i lavori Cron dovrebbero utilizzare il percorso completo:@hourly /usr/local/bin/brightness
-
Probabilmente il
@reboot
I processi Cron non funzioneranno con la versione corrente dello script.