21

How can I run the following xrandrcommand on startup?

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 
1
  • 1
    Hi Udhaya Kumar did you notice you have an answer? Please let me know if you manage. Jun 18, 2015 at 16:48

4 Answers 4

26

Adding complicated commands to Startup Applications

In General, you can add commands to run on start up (log in) by choosing: Dash > Startup Applications > Add. In this case, you have a complicated command to run.

There are two options to do that:

  1. write a separate script:

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

    Copy the script into an empty file, save it as set_monitor.sh and add the following command to startup applications as described above.

    /bin/bash /path/to/set_monitor.sh
    
  2. Chain the commands to one (very long) command:

     /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"
    

    In this case, using && between the commands will make each command run as soon (and if) the previous one is run succesfully, just like they are on separate lines.

    Then add the command to Startup Applications, as described above.

Important note: adding xrandr commands to Startup Applications

Adding xrandr commands to startup can be tricky; sometimes they break if they are run too early, before the desktop is fully loaded. Therefore you might (probably) need to add a little break into the command to (either) run the script or the command, like (in the last case):

/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"

You might need to play a little with the sleep 15to find the optimal time.

Note

I left out the first line:

xrandr

since it does nothin but display some information on your screen setup :)

7
  • 6
    You don't need to include the cvt command if you already know your modeline.
    – thethakuri
    Dec 16, 2016 at 7:49
  • 1
    The /bin/bash -c "..." wrapping did the trick for me :)
    – Superole
    Oct 3, 2017 at 13:22
  • 1
    On Kubuntu 17.10, I added the command without the sleep part in 'Startup applications' by selecting the "Before Session startup" option.
    – pat-s
    Jan 1, 2018 at 21:38
  • 1
    +1 for writing 3 years ago # xrandr only works in X11 sessions, not Wayland. Good future proofing back then. Sep 21, 2018 at 2:53
  • This solution finally worked in my Debian 10 with two monitors: Apple Thunderbolt 27 + LG 27MP59G. Natively, the LG supports only 1920x1080. With xrandr I set it up at the same resolution of the Tunderbolt of 2560x1440 with /bin/bash -c "sleep 3&&xrandr --output DP-2 --auto --scale-from 2560x1440".
    – f0nzie
    Mar 8, 2020 at 17:36
11

According to this at the Now automate it on login section, I have made my own script 45custom_xrandr-settings and placed it into /etc/X11/Xsession.d/. It works fine for me under Ubuntu 14.04 LTS. You could place the code below after the case command described in that section.

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;

I believe that the above is what you are looking for. You can see the available outputs simply by running the xrandr command. The outputs may be VGA, VGA-0, DVI-0, TMDS-1 or DisplayPort-0.

Here is the complete script that I made.

# 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
2
  • Thank you very much - your time and expertise is GREATLY appreciated! Thank you.
    – Duncan
    May 16, 2020 at 15:01
  • This method works perfectly, it just doesn't do it at the login screen but after you login and start a sesssion. I can live with that though.
    – robvdl
    Oct 28, 2020 at 19:33
4

Create the file ~/.xprofile and put your lines in it. It is ran at the beginning of the X user session.

3
  • 3
    this didnt work. commands fire up too early.
    – Necktwi
    May 27, 2017 at 6:33
  • @neckTwi Thanks, I found that xrandr --output ... doesn't work but the previous two lines works. All in all this works for me.
    – golopot
    May 28, 2017 at 5:45
  • .xsessionrc works for me - but I use xinit and not gdm/lightdm etc
    – RichieHH
    Feb 15, 2021 at 13:50
0

I was not able to get the script in /etc/x11/xsession.d to execute, however I used the method described to add the script to startup applications. I also added a sleep 15 line to the script just in case.

I used an xrandr command inside a text file in a hidden directory.

1
  • So you did what Jacob Vlijm wrote in his answer?
    – Levente
    Jun 16, 2023 at 22:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .