Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

migrated from serverfault.com Apr 22 '10 at 7:19

This question came from our site for professional system and network administrators.

2 Answers

up vote 6 down vote accepted

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:

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
share|improve this answer
Is there any way for me to specify that my script needs to run before gdm, without having to modify the gdm.conf script? I'd like to not have to apply my modifications again every time gdm gets an upgrade. –  Epcylon Apr 22 '10 at 17:01
@epcylon: see the way the gdm script specifies "start on" ... "and started hal"? that's referring to the /etc/init/hal.conf Upstart script. so ... hm... ok, lemme put the rest of this in the answer. –  quack quixote Apr 23 '10 at 3:06
Nice! Thank you! –  Epcylon Apr 23 '10 at 6:23
add comment

Normally, X is started in runlevel 5, and since you want your script to start before X (in form of xdm/kdm/gdm), you will need it to have a lower number then the Sxxxdm entry. So, on my system here, xdm is started with S111xdm, so you would need a symlink /etc/rc5.d/S10whereami.

share|improve this answer
I don't have an SXXxdm entry anywhere. I think I'm using gdm (the default for ubuntu), but I can't find a SXXgdm entry anywhere either. That's partly my problem.. –  Epcylon Apr 21 '10 at 20:31
Ah, yes, Ubuntu since 9.10 means Upstart, works diffferent than traditional SysV init, but I don't know how exactly (didn't use it yet), so you should really move this question to superuser.com :) –  SvW Apr 21 '10 at 21:34
@epcylon: ubuntu follows the debian runlevel config: runlevel 1 is single-user, and runlevels 2-5 are all the same (GUI multi-user). by default ubuntu boots to runlevel 2. –  quack quixote Apr 22 '10 at 8:55
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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