You are not logged in.

#1 2020-08-19 23:10:00

tda
Member
Registered: 2020-06-27
Posts: 44

[SOLVED] auto start X, error following the Gnome Arch Wiki page

I am trying to automatically start X at login, and am getting an error trying to follow the Arch Wiki. I have set up my ~/.xinitrc file according to the Gnome Arch Wiki page, and it works when I manually run startx. But I'm having some trouble automatically starting X when I log in. The Gnome Arch Wiki page suggests I add the following to my ~/.bash_profile:

if [[ -z $DISPLAY && $(tty) == /dev/tty2; ]]; then
  XDG_SESSION_TYPE=x11 GDK_BACKEND=x11 exec startx
fi

But if I do that, I get the error

-bash: /home/trevor/.bash_profile: line 7: syntax error in conditional expression: unexpected token `;'
-bash: /home/trevor/.bash_profile: line 7: syntax error near `;'
-bash: /home/trevor/.bash_profile: line 7: `if [[ -z $DISPLAY && $(tty) == /dev/tty2; ]]; then'

I'm not a shell scripting expert, but I got that code to work by removing the semicolon after tty2 (and replacing tty2 with tty1). Also, that page links to the Autostart X at login section of the Xinit page, which has a different instruction, and says that you should add this to ~/.bash_profile:

if systemctl -q is-active graphical.target && [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]]; then
  exec startx
fi

This command works for me, so is the first instruction incorrect? And why are the two instructions so different?

Last edited by tda (2020-08-24 01:26:19)

Offline

#2 2020-08-20 00:24:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,517
Website

Re: [SOLVED] auto start X, error following the Gnome Arch Wiki page

tda wrote:

I'm not a shell scripting expert, but I got that code to work by removing the semicolon after tty2

Your closer to expert than whoever put that command in the wiki.  If you see different advice on multiple pages, follow the more common/primary/well-reviewed page.  The semicolon after tty2 is wrong.

tda wrote:

This command works for me, so is the first instruction incorrect? And why are the two instructions so different?

If it works, it works.  Although that 'systemctl -q ...' condition has been known to cause problems as well - when it works, it essentially works by accident.  When it does what the command is really supposed to do, it will fail to work.

The wiki used to have a much more robust and sane conditional:

if [[ -n $DISPLAY && "$XDG_VTNR" -eq 1 ]]; then

But each variant makes ever so slightly different assumptions about how your system is running.  In the end, it's your system.  Learn a little shell scripting to understand what the conditionals are doing and ensure you are happy with the criteria you use in the conditional.

------------------------------
EDIT for some details:

Each of these conditionals in principle are designed to check 1) that X is not already running on the current tty (i.e., don't start X within X), and 2) only start X on a specified tty.  X can be started just fine on multiple ttys, but most people would not want this as it would make it very hard to impossible to log in to an actual tty if something goes wrong with X.  So the templates provided show how to start X on a specific tty (in most cases, tty1 ... I don't know why the author of the gnome page used tty2, but that'd be fine to, it just wouldn't autostart at boot up).

For each of the two conditions above, there are several means of testing.  The first is most clearly checked by testing if the DISPLAY variable is non-empty: `[[ -n $DISPLAY ]]` in bash, or `[ -n "$DISPLAY" ]` for POSIX compliance.  The systemctl command is intended to acheive the same result, but it is subject to a race condition and can fail in odd and inconsistent ways - someone else may be able to make a case for it, but it is just a bad idea in my book.

For checking which tty you are on, systemd/logind has a handy environment variable it sets XDG_VTNR, so if you (always) use systemd as your init, then by all means use that: `[[ $XDG_VTNR -eq 1 ]]` to check whether the VTNR (virtual terminal number) is 1 - or in otherwords you are on tty1.  If you might tinker with other init systems, they will not set that environment variable, so you'd need to use the binary `tty` to check which tty you're on: `[[ $(tty) == /dev/tty1 ]]`  While this option is more portable, it does start another subshell and run an external program ... these are really trivial in day to day life, but if you're as maniacal as I am about avoiding these when possible, the XDG_VTNR is better if you only use systemd.

Lastly, there are some particulars I mostly glossed over above that differ between the BASH [[ conditional test and the POSIX-compliant [ test.  As I tinker with other init systems, and I don't use BASH, my profile includes this conditional:

[ $(tty) == /dev/tty1 ] && [ -z "$DISPLAY" ] && exec startx             # I don't actually use startx, but this is my conditional.

Last edited by Trilby (2020-08-20 00:39:17)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2020-08-20 06:35:07

seth
Member
Registered: 2012-09-03
Posts: 50,933

Re: [SOLVED] auto start X, error following the Gnome Arch Wiki page

ftr, the "systemctl -q is-active graphical.target" check migth cause trouble.

https://bbs.archlinux.org/viewtopic.php?id=253417
https://bbs.archlinux.org/viewtopic.php?id=257944

Offline

#4 2020-08-20 18:02:40

tda
Member
Registered: 2020-06-27
Posts: 44

Re: [SOLVED] auto start X, error following the Gnome Arch Wiki page

Thanks for all the great information! One more question: In the first ~/.bash_profile snippet I posted from the Gnome Arch Wiki page, the XDG_SESSION_TYPE and GDK_BACKEND variables are set. But earlier in the page it advises setting those in ~/.xinitrc. So wouldn't it be redundant to set them again in ~/.bash_profile?

Offline

#5 2020-08-20 18:10:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,517
Website

Re: [SOLVED] auto start X, error following the Gnome Arch Wiki page

Yes, it would be redundant.  Either one should suffice.  Xinitrc would seem the better place to me.

Note, I edited that section to fix the actual error, but I stopped short of making it "good" as that whole section just seems poorly done to me.  My activity on the wiki is almost non-existent - it's just not my preferred mode of involvement.  So without knowing the wiki-editing culture I didn't want to step into any controversy there by substantially chopping up a section for what one might argue were subjective reasons (the semicolon out of place was about as concrete and objective as it can get, so I was happy to make that change).

Personally I'd say that entire blurb about autostarting X simply has no business on that page at all - there is already a general page for that and there is absolutely nothing specific to gnome about it.

Last edited by Trilby (2020-08-20 18:13:06)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2020-08-21 04:22:08

tda
Member
Registered: 2020-06-27
Posts: 44

Re: [SOLVED] auto start X, error following the Gnome Arch Wiki page

Ok, thanks again and thanks for making that edit. Maybe I'll post something in the Discussion section of the Gnome ArchWik page about it.

Offline

Board footer

Powered by FluxBB