Ubuntu – Run script on X11 startup

startupxorg

I had a problem with Caps and I've got a script to fix this. Unfortunately, this script works until reboot:

# Fix caps
xkbcomp -xkb "$DISPLAY" - | sed 's#key <CAPS>.*#key <CAPS> {\
    repeat=no,\
    type[group1]="ALPHABETIC",\
    symbols[group1]=[ Caps_Lock, Caps_Lock],\
    actions[group1]=[ LockMods(modifiers=Lock),\
    Private(type=3,data[0]=1,data[1]=3,data[2]=3)]\
};\
#' | xkbcomp -w 0 - "$DISPLAY"
exit 0

I would just add it into startup applications, but I'd like to run it on LightDM startup, or even at X11 start. Is there any way to run this script on X11 startup? I'm using Xubuntu 16.04.2.

Thanks.

Best Answer

Try the following:

  1. Place your script into a system-wide directory such as /usr/local/bin with an appropriate shebang

    #!/bin/sh
    
    # Fix caps
    /usr/bin/xkbcomp -xkb "$DISPLAY" - | /bin/sed 's#key <CAPS>.*#key <CAPS> {\
        repeat=no,\
        type[group1]="ALPHABETIC",\
        symbols[group1]=[ Caps_Lock, Caps_Lock],\
        actions[group1]=[ LockMods(modifiers=Lock),\
        Private(type=3,data[0]=1,data[1]=3,data[2]=3)]\
    };\
    #' | xkbcomp -w 0 - "$DISPLAY"
    exit 0
    
  2. Make it executable e.g. sudo chmod +x /usr/local/bin/fix-caps.sh

  3. Create a custom config file in /etc/lightdm/lightdm.conf.d. Use a name such as 99-local-settings.conf. (The 99 prefix means that it will be run after the standard setup scripts provided in /usr/share/lightdm/lightdm.conf.d.)

    Add the location of your script as a display-setup-script int the [SeatDefaults] section:

    [SeatDefaults]
    display-setup-script = /usr/local/bin/fix-caps.sh
    

The display-setup-script should be run after the X server is started, but before any greeter is run - see LightDM: Adding system hooks.

Related Question