Running a script on startup before X starts in Ubuntu 9.10

I have a script that I want to run at startup to switch X-configs depending on location, but I can't seem to find out where to put it in order to get it to run before X is started. This results in me having to restart X to get it to run the correct config.

Currently, my script is located in /etc/init.d/whereami, with symlinks in /etc/rc[2-5].d/S25whereami. I was trying to find out when X is started, in case the problem is simply the 25, but I can't seem to find the answer...

Any help is appreciated.


Solution 1:

Ubuntu starts X-windows from GDM (for systems using the standard ubuntu-desktop) or from KDM (for systems using kubuntu-desktop).

To run a script prior to GDM/KDM startup, you could either

  1. Write your own Upstart script to run prior to GDM; or
  2. Modify your system GDM script to run a custom external script (eg, your /etc/init.d/whereami script) as one of its initial tasks.

Ubuntu uses Debian-style runlevels: runlevel 1 is single-user, recovery mode; runlevels 2-5 are all the same (GUI multi-user); and by default the system boots to runlevel 2.

Ubuntu 9.10 uses Upstart, which provides service startup scripts in /etc/init. The more traditional init scripts in /etc/init.d and /etc/rc.d are still available for services that haven't been ported to Upstart-style init scripts, but GDM has one. (Note Ubuntu 9.10 provides /etc/init.d/gdm but doesn't create any /etc/rcX.d symlinks for it.)

GDM startup is controlled by /etc/init/gdm. These lines at the top of the script specify when to start and stop GDM. Notice there's no runlevel specification for starting GDM -- only services that must be started first.

start on (filesystem
          and started hal
          and tty-device-added KERNEL=tty7
          and (graphics-device-added or stopped udevtrigger))
stop on runlevel [0156]

emits starting-dm

See man 5 init for documentation on Upstart scripts, and examine your system's /etc/init/*.conf files for some examples. Also see the starting event manpage:

Example
A service that wishes to be running whenever another service would be running, started before and stopped after it, might use:

start on starting apache
stop on stopped apache

A task that must be run before another task or service is started might use:

start on starting postgresql

Also note that the GDM/KDM Upstart scripts emit a signal, starting-dm, that might also be useful.

So an Upstart script designed to run before GDM should use one of these start on clauses:

# run only when starting GDM
start on starting gdm

# run when starting GDM or KDM
start on (starting gdm 
          or starting kdm)

# run when starting any DM
# starting-dm is a custom event emitted by the GDM/KDM/etc scripts
start on starting-dm