I have a working automatic login to a virtual console after boot. Now I want the X server to start automatically, instead of typing startx. This worked before, but after switching to systemd, X would not start itself. According to the wiki page, I edited my ~/.bash_profile:

# ~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx

But X won´t start (executing startx manually works).

The wiki also notes:

X must always be run on the same tty where the login occurred, to preserve the logind session. This is handled by the default /etc/X11/xinit/xserverrc.

But I am not sure how to handle this. My /etc/X11/xinit/xserverrc:

if [ -z "$XDG_VTNR" ]; then
  exec /usr/bin/X -nolisten tcp "$@"
  exec /usr/bin/X -nolisten tcp "$@" vt$XDG_VTNR

What am I doing wrong?



# ~/.xinitrc
# Executed by startx (run your window manager from here)

# Keyboard layout
setxkbmap -layout de nodeadkeys
# Set the cursor
xsetroot -cursor_name left_ptr

# Autostart
tint2 & #Taskbar
/usr/lib/notification-daemon-1.0/notification-daemon & #Notifications
numlockx & #activate numlock

case "$1" in
        exec ck-launch-session openbox-session ;;
        exec ck-launch-session xmonad ;;
    *) #default
        exec ck-launch-session xmonad ;;

I don´t have a ~/.Xresources file and I didn´t need one before. Is it important?

  • Remove the consolekit stuff from your .xinitrc; systemd has replaced that functionality... – jasonwryan Jan 31 '13 at 17:16
  • Curious why you don't use one of the X display managers and set auto-login there. KDM and GDM both have auto-login (if you're already using one of those desktop environments). Not sure about XDM. Or use nodm, which does what you want and is pretty minimal. – derobert Jan 31 '13 at 17:23

I'm not sure what XDG_VTNR implicates, but this script works for me, put the line to ~/.bashrc

(I do auto-X on tty3)

if [[ -z $DISPLAY ]] && [[ $(tty) = /dev/tty3 ]]; then exec startx; fi

Also define "not working", does it work if you launch startx manually? If not, examine your ~/.xinitrc first.

  • Executing startx manually works, updated question. – user905686 Jan 27 '13 at 10:27
  • @user905686 check this link bbs.archlinux.org/viewtopic.php?pid=1188688 – daisy Jan 27 '13 at 10:55
  • this doesn´t really help... – user905686 Jan 27 '13 at 18:22
  • Ok I tried this now and it works. Any idea why the wiki instructions do not work? Can you also explain what your command does? The second part checks whether we are on tty3 so startx will really be executed only once, right? – user905686 Mar 11 '13 at 12:10

In .bash_profile, you can find out (after autologin with getty -a in /etc/inittab) what tty you are in with tty (if you're in fact in X, it could look like this: /dev/pts/1 - this is what the p check is about below). Then, set a variable to store this information; later, use it to launch X with xinit.

You should also setup ~/.xinitrc and ~/.Xresources; a good start of ~/.xinitrc is xrdb ~/.Xresources - check the man pages for all of this. You can find some examples here.

if [[ ${CURRENT_VT[6]} == "p" ]]; then
    export VT="X"
    export VT=${CURRENT_VT: -1:1}

# ... [cut] initialize everything else ...

# OK, done; let's start applications and X depending on where we are

case $VT in
    (1) emacs ;;
    (5) wait_for_internet
        irssi --connect=open.ircnet.net ;;
    (6) xinit ;;
    (X) ... do X stuff ... ;;


By mistake, I posted the above zsh code - in bash syntax, it would look somewhat like this (with the reservation that it is much less tested than the zsh code, which runs every day on my computer). The rest is unchanged.

if [[ ${CURRENT_VT:5:1} = "p" ]]; then
    export VT="X";
    export VT=${CURRENT_VT:${#CURRENT_VT} - 1};
  • @Gilles: It seems to work in .bashrc as well (when I changed the syntax; see the edit) - the reason I thought it would go there, is that I have it in .zshrc - so either those files are not 100% parallel, or you don't think the above belongs in .zshrc either - I sort of suspect the latter (as there is a .zprofile), so it would be interesting to know why? – Emanuel Berg Jan 28 '13 at 1:20
  • If you put this code in .bashrc, it'll trigger in all kinds of undesirable circumstances, such as running screen, or SSH if it's forwarding the XDG_VTNR variable, or in an xterm if you've unset DISPLAY, etc. – Gilles 'SO- stop being evil' Jan 28 '13 at 1:22
  • @Gilles: Is this the case for zsh as well? (That is, you should use .zprofile?) – Emanuel Berg Jan 28 '13 at 1:28
  • Yes, no matter what the shell is, starting X goes into the login script (.profile, .login, .bash_profile, .zprofile, …), not the interactive startup script (.bashrc, .kshrc, .cshrc, .zshrc, …). – Gilles 'SO- stop being evil' Jan 28 '13 at 1:35
  • @Emanuel Berg: I thought you should use startx and not xinit? Updated my question with other files. – user905686 Jan 31 '13 at 17:04

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.