I have two xorg.conf files that I use, depending on where I am. Lately I got sick of switching them manually like this:

sudo cp /etc/X11/xorg.conf.h /etc/X11/xorg.conf
sudo pkill X

So I decided to add switching script to rc.local file. Unfortunately rc.local seems to start after all other services. Of course this would work with sudo pkill X included. But to be honest, killing X every time I turn on my computer is a bit lame.

Run levels to the rescue

You can read about run levels here. I’ll just tell you that you need to check your current run level by running this command:

runlevel

You’ll get something like this:

[~]$ runlevel 
N 2
[~]$ 

My Ubuntu is by default using run level 2. So now I know that I need to hookup before X starts in this run level. To do this, you need to create a script in /etc/rc2.d/ directory. If you do a ls -al in any or RC dirs, you’ll see that all the scripts there are just symlinks to /etc/init.d/ scripts. It is convenient to store all of them there, because they might be reused in other run levels.

Naming convention

All scripts (aliases) that should be executed in given run level, have a ‘SNUMBER’ prefix. S probably states for “Start” and number determines the order in which all the scripts well be executed (well there is also LSB that might “disrupt” the specified order but fortunately not in our case).

Example of rc2.d:

lrwxrwxrwx   1 root root    15 2012-04-25 16:42 S20mysql -> ../init.d/mysql
lrwxrwxrwx   1 root root    15 2011-06-29 09:55 S20nginx -> ../init.d/nginx
lrwxrwxrwx   1 root root    17 2011-11-14 21:25 S20postfix -> ../init.d/postfix
lrwxrwxrwx   1 root root    22 2011-06-29 02:03 S20redis-server -> ../init.d/redis-server
lrwxrwxrwx   1 root root    16 2012-04-28 21:51 S20tcpspy -> ../init.d/tcpspy
lrwxrwxrwx   1 root root    17 2011-06-22 01:18 S20vboxdrv -> ../init.d/vboxdrv
lrwxrwxrwx   1 root root    17 2011-06-21 22:40 S20winbind -> ../init.d/winbind
lrwxrwxrwx   1 root root    19 2011-06-22 00:05 S25bluetooth -> ../init.d/bluetooth
lrwxrwxrwx   1 root root    20 2011-06-22 00:05 S50pulseaudio -> ../init.d/pulseaudio
lrwxrwxrwx   1 root root    15 2011-06-22 00:05 S50rsync -> ../init.d/rsync
lrwxrwxrwx   1 root root    15 2011-06-22 00:05 S50saned -> ../init.d/saned
lrwxrwxrwx   1 root root    19 2011-06-22 00:05 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx   1 root root    18 2011-06-22 00:05 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx   1 root root    14 2012-05-27 19:33 S75sudo -> ../init.d/sudo
lrwxrwxrwx   1 root root    17 2011-06-21 22:39 S91apache2 -> ../init.d/apache2
lrwxrwxrwx   1 root root    22 2011-06-22 00:05 S99acpi-support -> ../init.d/acpi-support
lrwxrwxrwx   1 root root    21 2011-06-22 00:05 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx   1 root root    18 2011-06-22 00:05 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx   1 root root    18 2011-06-22 00:05 S99rc.local -> ../init.d/rc.local

X conf switchin script

I’ve named this script ‘xselector’ and I’ve placed it in /etc/init.d with rest of scripts. Remember to give this script execution rights!

#!/bin/sh
video_home(){
  rm /etc/X11/xorg.conf
  cp /etc/X11/xorg.conf.dom /etc/X11/xorg.conf
}

video_work(){
  rm /etc/X11/xorg.conf
  cp /etc/X11/xorg.conf.praca /etc/X11/xorg.conf
}

DAY=$(date +"%u")
HOUR=$(date +"%H")

# If this is a work day
if [ "$DAY" -lt 6 ]; then
  # And these are hours when I'm @ work
  if [ "$HOUR" -gt 7 -a "$HOUR" -lt 18 ]; then
    video_work
  else
    video_home
  fi
else
  video_home
fi

After we create our script we just need to add it like this:

sudo ln -s /etc/init.d/xselector /etc/rc2.d/S15xselector