使用xrandr和cron作业调整亮度

编辑感谢pa4080我在下面的脚本中添加了一行,现在效果很好。 我不太清楚如何,哦。

我想做一个cron工作来调整我在一天中不同时间的亮度。 在做了一些谷歌搜索和反复试验之后,我编写了以下适用的bash脚本:

#!/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 

然后我使用crontab -e添加以下行:

 0 * * * * /home/piney/screendimmer.sh 

触发了cronjob,但脚本没有运行。 我究竟做错了什么?

Cron默认提供有限的环境变量集[1] 。 要让xrandr通过Cron作业,你应该导出[2]当前用户的$DISPLAY变量[3]的值 。 为此,将以下行添加到脚本的开头(或将其添加到crontab文件[4]中 ):

 export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}') 

参考文献:

  • 应该执行到终端窗口的Crontab和C程序

  • 如何在取消设置DISPLAY时以编程方式查找DISPLAY的当前值?


我喜欢这个想法,并且已经在我的系统中实现了它。 这是我上面脚本的版本:

 #!/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 ""' fi 
  • 该脚本能够自动获取活动监视器的列表。 我用两台显示器对它进行了测试。

  • 不错的想法是将可执行文件[5]放在/usr/local/bin ,因此它也可以作为shell命令使用。 我们假设它被称为brightness

  • 该脚本可以使用一个参数,该参数将覆盖默认的亮度值,例如: brightness .9

  • 虽然/usr/local/bin未在crontab$PATH variable [1] [4] [6]中列出,但Cron作业应使用完整路径:

     @hourly /usr/local/bin/brightness 
  • 可能@reboot Cron作业不适用于当前版本的脚本[7]

您必须键入安装xrandr的路径。 键入command -v xrandr (或which xrandr )以了解它的安装位置。 我想它是/usr/bin/xrandr ,如果它是默认安装的话。

所以,编辑你的crontab所以:

 #!/bin/bash H=$(date +%k) if (( $H > 0 && $H <= 7 )); then /usr/bin/xrandr --output HDMI-1 --brightness .3 && /usr/bin/xrandr --output HDMI-2 --brightness .3 && /usr/bin/xrandr --output HDMI-3 --brightness .3 elif (( $H > 7 && $H <= 10 )); then /usr/bin/xrandr --output HDMI-1 --brightness .5 && /usr/bin/xrandr --output HDMI-2 --brightness .5 && /usr/bin/xrandr --output HDMI-3 --brightness .5 elif (( $H > 10 && $H <= 19 )); then /usr/bin/xrandr --output HDMI-1 --brightness .7 && /usr/bin/xrandr --output HDMI-2 --brightness .7 && /usr/bin/xrandr --output HDMI-3 --brightness .7 elif (( $H > 19 && $H <= 22 )); then /usr/bin/xrandr --output HDMI-1 --brightness .5 && /usr/bin/xrandr --output HDMI-2 --brightness .5 && /usr/bin/xrandr --output HDMI-3 --brightness .5 elif (( $H > 22 && $H <= 23 )); then /usr/bin/xrandr --output HDMI-1 --brightness .3 && /usr/bin/xrandr --output HDMI-2 --brightness .3 && /usr/bin/xrandr --output HDMI-3 --brightness .3 else echo "Error" fi 

您可能想要查看redshift ,而不是编写cron作业来手动更改显示器的亮度, redshift是一个可以完成此操作的程序。 它可以设置为跟踪您所在位置的日光,并更改显示屏的亮度和色温,以更好地匹配自然光。

图像说明了红移的影响

它的主要卖点是改变色温(即将颜色更多地转向红色,这就是名称的来源),但它也可以调节亮度。 您可以将其配置为仅执行亮度,如果这是您想要的。

与手动解决方案相比,主要优势在于红移逐渐改变颜色/亮度,与您所在位置的当前每日周期相匹配,而不是像您的cron方法那样逐步改变。 您也可以轻松地打开/关闭效果; 发送进程SIGUSR1将切换效果。 我做了一个killall -USR1 redshift绑定,使killall -USR1 redshift使这很容易访问。

还有另一个类似function的程序叫f.lux ,它也支持Windows和MacOS,看起来很受欢迎。 我没有经验; 特别是我不完全确定它是否可以改变亮度以及色温。

如果你像这样使用xbacklight另一个选择就是使用xbacklightxrandr --output HDMI-1 --brightness .3 && xrandr --output HDMI-2 --brightness .3 && xrandr --output HDMI-3 --brightness .3如果你有VGA输出,这个命令将失败。

您可以使用sudo apt install xbacklight安装它。 我使用xbacklightxbacklight合作,他们是最好的。