1

I am trying to find a way to automate launching a shell script after Xubuntu logs in.
I've checked numerous other questions and answers and have still not found a way to do it.

Here is the script that I am trying to run:

#!/bin/sh

SH_PATH="`dirname \"$0\"`"
xkbcomp ${SH_PATH}/keyboardmap $DISPLAY

It is a script based on the Linux CapsLock Delay Fixer that modifies the caps lock key to behave like it does on Windows platform so that it can be used to capitalize while typing fast.

I've tried creating an entry in Session and Startup -> Application Autostart

Session and Startup

Here are the things I've tried:

/home/yooks/Scripts/fix_capslock.sh
sh /home/yooks/Scripts/fix_capslock.sh
sh -c "sh /home/yooks/Scripts/fix_capslock.sh"
sh -c "/home/yooks/Scripts/fix_capslock.sh"

The script executes fine when I run it from terminal on any path.
What is the easiest way of making this script run whenever I login to Xubuntu?

1

Depending on what DM (Display Manager) you are using will determine what the $DISPLAY is set to. Since your script is actually running without a shell at the time of login, there is no $DISPLAY set. To remedy this, you may want to add the following lines below the #!/bin/sh line in your script:

#Get Display Manger
DM=$(/usr/bin/basename $(/bin/cat /etc/X11/default-display-manager))

#Set Display based on DM and login type
case $DM in
    lightdm)
        export DISPLAY=:0;;
    gdm3)
        grep -E "# AutomaticLogin|AutomaticLoginEnable = false" /etc/$DM/*.conf >/dev/null && export DISPLAY=:1 || export DISPLAY=:0;;
    *);;
esac
        

Through testing I found that when I log into my system with LightDM, the Display has been always :0 regardless if automatic login or not. But with GDM3 if you have automatic login setup it has been :0 but if you don't have automatic login it has been :1. I run a script that launches Firefox automatically at certain times, and adding this to set the $DISPLAY has made it so Firefox has launched from the script without it running in a terminal at the time.

Hope this helps!

6
  • Thanks for the answer, I will try it out. I still need to know how to define the script to be automatically run after logging in. My system is configured to automatically login.
    – Matthew
    Apr 27 at 19:01
  • @Matthew The way you added it in Session and Startup is correct.
    – Terrance
    Apr 27 at 19:03
  • I've defined it in a few different ways, which ones are correct?
    – Matthew
    Apr 27 at 19:04
  • @Matthew Since you have #!/bin/sh in the script itself, you don't need the sh -c in front of it, but it doesn't matter if it is there. The only time I use sh -c is if I put a sleep in front of the script so it delays for like 5 seconds at startup. i.e. sh -c 'sleep 5 && /path/to/script.sh'
    – Terrance
    Apr 27 at 19:06
  • 1
    Thank you very much, your solution seems to have worked. I've would have never guessed that the problem was with $DISPLAY variable. I just thought I am defining the autostart entry wrong in GUI.
    – Matthew
    Apr 27 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.