如何在 Ubuntu 启动时运行 xrandr 命令

Udh*_*mar 17 xrandr command-line display startup-applications display-resolution

如何xrandr在启动时运行以下命令?

xrandr

cvt 1368 768 
xrandr --newmode "1368x768_60.00"   85.25  1368 1440 1576 1784  768 771 781 798 -hsync +vsync
xrandr --addmode VGA1 1368x768_60.00
xrandr --output VGA1 --mode 1368x768_60.00 
Run Code Online (Sandbox Code Playgroud)

Jac*_*ijm 25

向启动应用程序添加复杂的命令

通常,您可以通过选择:Dash > 启动应用程序 > 添加来添加要在启动(登录)时运行的命令。在这种情况下,您需要运行一个复杂的命令。

有两种选择可以做到这一点:

  1. 写一个单独的脚本:

    #!/bin/bash
    
    cvt 1368 768 
    # xrandr only works in X11 sessions, not Wayland
    [ "$XDG_SESSION_TYPE" = x11 ] || exit 0
    xrandr --newmode "1368x768_60.00"   85.25  1368 1440 1576 1784  768 771 781 798 -hsync +vsync
    xrandr --addmode VGA1 1368x768_60.00
    xrandr --output VGA1 --mode 1368x768_60.00
    
    Run Code Online (Sandbox Code Playgroud)

    将脚本复制到一个空文件中,将其另存为set_monitor.sh 并将以下命令添加到上述启动应用程序中。

    /bin/bash /path/to/set_monitor.sh
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将命令链接到一个(很长的)命令:

     /bin/bash -c "cvt 1368 768&&xrandr --newmode "1368x768_60.00"   85.25  1368 1440 1576 1784  768 771 781 798 -hsync +vsync&&xrandr --addmode VGA1 1368x768_60.00&&xrandr --output VGA1 --mode 1368x768_60.00"
    
    Run Code Online (Sandbox Code Playgroud)

    在这种情况下,&&在命令之间使用将使每个命令在(如果)前一个命令成功运行后立即运行,就像它们在单独的行上一样。

    然后将命令添加到启动应用程序,如上所述。

重要说明:将 xrandr 命令添加到启动应用程序

xrandr启动添加命令可能很棘手;有时,如果在桌面完全加载之前运行太早,它们就会中断。因此,您可能(可能)需要在命令中添加一些中断以(或者)运行脚本或命令,例如(在最后一种情况下):

/bin/bash -c "sleep 15&&cvt 1368 768&&xrandr --newmode "1368x768_60.00"   85.25  1368 1440 1576 1784  768 771 781 798 -hsync +vsync&&xrandr --addmode VGA1 1368x768_60.00&&xrandr --output VGA1 --mode 1368x768_60.00"
Run Code Online (Sandbox Code Playgroud)

您可能需要稍微调整一下sleep 15以找到最佳时间。

笔记

我省略了第一行:

xrandr
Run Code Online (Sandbox Code Playgroud)

因为它只会在您的屏幕设置上显示一些信息:)

  • 如果您已经知道您的模式行,则不需要包含 `cvt` 命令。 (6认同)

ioa*_*atr 9

根据这个现在,它自动在登录部分,我已经作出了自己的脚本45custom_xrandr-settings,并使其进入/etc/X11/Xsession.d/。它在 Ubuntu 14.04 LTS 下对我来说很好用。您可以将下面的代码case放在该部分中描述的命令之后。

PRI_OUTPUT="DVI-0";
# Make and force resolution
myNewMode=$(cvt 1366 768 60 | grep -oP 'Modeline\K.*') &&                           #grep evrything after 'Modline'
myNewModeName=\"$(echo $myNewMode | grep -oP '"\K[^"\047]+(?=["\047])' )\" &&       #grep everything inside quotes
xrandr --newmode $myNewMode;
sleep 15;       
xrandr --addmode $PRI_OUTPUT $myNewModeName;
Run Code Online (Sandbox Code Playgroud)

我相信以上就是你要找的。您只需运行该xrandr命令即可查看可用输出。该输出可以是VGAVGA-0DVI-0TMDS-1DisplayPort-0

这是我制作的完整脚本。

# To configure xrandr automatically during the first login, 
# save this script to your computer as /etc/X11/Xsession.d/45custom_xrandr-settings: 

# If an external monitor is connected, place it with xrandr
# External output may be "VGA" or "VGA-0" or "DVI-0" or "TMDS-1"

# More info at http://www.thinkwiki.org/wiki/Xorg_RandR_1.2


PRI_OUTPUT="DVI-0";
SEC_OUTPUT="DisplayPort-0";
SEC_LOCATION="left";    # SEC_LOCATION may be one of: left, right, above, or below

case "$SEC_LOCATION" in
       left|LEFT)
               SEC_LOCATION="--left-of $PRI_OUTPUT"
               ;;
       right|RIGHT)
               SEC_LOCATION="--right-of $PRI_OUTPUT"
               ;;
       top|TOP|above|ABOVE)
               SEC_LOCATION="--above $PRI_OUTPUT"
               ;;
       bottom|BOTTOM|below|BELOW)
               SEC_LOCATION="--below $PRI_OUTPUT"
               ;;
       *)
               SEC_LOCATION="--left-of $PRI_OUTPUT"
               ;;
esac

# Make and force resolution
myNewMode=$(cvt 1366 768 60 | grep -oP 'Modeline\K.*') &&                           #grep evrything after 'Modline'
myNewModeName=\"$(echo $myNewMode | grep -oP '"\K[^"\047]+(?=["\047])' )\" &&       #grep everything inside quotes
xrandr --newmode $myNewMode;
sleep 15;       
xrandr --addmode $PRI_OUTPUT $myNewModeName;


# Activate secondary out (display port)
xrandr | grep $SEC_OUTPUT | grep " connected "
if [ $? -eq 0 ]; then
#   xrandr --output $SEC_OUTPUT --auto $SEC_LOCATION
    xrandr --output $PRI_OUTPUT --mode $myNewModeName --output $SEC_OUTPUT --auto $SEC_LOCATION
else
    xrandr --output $PRI_OUTPUT --mode $myNewModeName --output $SEC_OUTPUT --off
fi
Run Code Online (Sandbox Code Playgroud)