Pin*_*ney 6 xrandr bash scripts cron brightness
编辑感谢 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
Run Code Online (Sandbox Code Playgroud)
然后我使用 crontab -e 添加以下行:
0 * * * * /home/piney/screendimmer.sh
Run Code Online (Sandbox Code Playgroud)
cronjob 被触发,但脚本没有运行。我究竟做错了什么?
Cron 默认提供有限的环境变量集[1]。要完成Cron 作业,您应该导出[2]当前用户的变量[3] 的值。为此,请将以下行添加到脚本的开头(或将其添加到文件[4] 中): xrandr $DISPLAY crontab
export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}')
Run Code Online (Sandbox Code Playgroud)
参考:
我喜欢这个想法并且已经在我的系统中实现了它。这是我上面脚本的版本:
#!/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
Run Code Online (Sandbox Code Playgroud)
该脚本能够自动获取活动监视器的列表。我已经用两台显示器对其进行了测试。
好主意是将可执行文件[5]放在 中,因此它也可以作为 shell 命令使用。让我们假设它被称为。 /usr/local/binbrightness
该脚本能够使用的参数,这将覆盖默认的亮度值,例如:brightness .9。
而/usr/local/bin在未列出crontab的[1] [4] [6]中,Cron作业应该使用的全路径:$PATH variable
@hourly /usr/local/bin/brightness
Run Code Online (Sandbox Code Playgroud)也许@rebootCron作业不会与脚本当前版本的工作[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
Run Code Online (Sandbox Code Playgroud)
您可能需要查看redshift,而不是编写 cron 作业来手动更改显示器的亮度,这是一个可以做到这一点的程序。它可以设置为跟踪您所在位置的日光,并更改显示器的亮度和色温以更好地匹配自然光。
它的主要卖点是改变色温(即,将颜色更偏向红色,这就是名称的来源),但它也可以调节亮度。如果这是你想要的,你可以将它配置为只做亮度。
与手动解决方案相比,红移的主要优点是逐渐改变颜色/亮度,与您所在位置的当前每日周期相匹配,而不是像您的 cron 方法那样逐步改变。您还可以相当轻松地打开/关闭效果;发送进程 SIGUSR1 将切换效果。我做了一个键绑定,killall -USR1 redshift使这很容易访问。
还有另一个类似功能的程序叫做f.lux,它也支持 Windows 和 MacOS 并且似乎很受欢迎。我没有这方面的经验;特别是我不完全确定它是否可以改变除了色温之外的亮度。
| 归档时间: |
|
| 查看次数: |
2194 次 |
| 最近记录: |