Mike Baker Mike Baker - 1 month ago 9
Linux Question

How do I automatically start an application that needs X in Linux

I'm trying to start an X application automatically when the system enters run level 5.

What is the correct way to do this?

I've written a script and put it in /etc/init.d/

I've run the appropriate chkconfig commands to setup up the symbolic links in the /etc/rcX.d directories.

Everything works fine except the script can't start the X application when I run:

/sbin/service scriptName start


The line looks like this (I've switched in xclock for the sake of argument):

'start')
xclock


I get the following error:

Error: Can't open display:


After some research it appears that /sbin/service removes most of the environment variables from a typical shell. By adding DISPLAY:

'start')
DISPLAY=:0
export DISPLAY
xclock


I get:

Error: Can't open display: :0


I modified my script to include:

'start')
DISPLAY=:0
export DISPLAY
XAUTHORITY=/root/.xauth2w90ge
export XAUTHORITY
xclock


And now it works - but this is obviously a hack as the .xauth2w90ge file is generated and won't be the same from instance to instance.

So is there a proper way to do this, or am I barking up the wrong tree? My requirement is to have this application autostart with the machine and I am limited by whatever commands and permissions I can run in an RPM postinstall script.

Answer

Rather than defining an init script, you should be having X (or your window manager) start the process automatically. X, KDE, and Gnome all have ways of automatically starting things up (i.e. ~/.kde4/Autostart).

If this IS just X, go modify your /etc/X11/xinit/xinitrc file (or equivalent) to have it run your command. Mine looks like this at the bottom of the file:

if [ -n "$failsafe" ]; then
    twm &
    xclock -geometry 50x50-1+1 &
    xterm -geometry 80x50+494+51 &
    xterm -geometry 80x20+494-0 &
    exec xterm -geometry 80x66+0+0 -name login
else
    exec $command
fi

So you would change that to run whatever command you want.

Comments