You are not logged in.

#1 2024-05-26 19:02:13

zaxdan69
Member
Registered: 2016-06-04
Posts: 296

Help with auto-login script

I'm using xinit and autologin to start a desktop environment/window manager and I have a case with some options. What I'm trying to do is to use a script with option to choose one of them, which I want to have a timeout before load the default option. I don't have much experience in bash scripting and I used chatgpt to help me achive this, but seems that cannot help and loops to the same suggestions which don't work.
So, I have a script which I use to choose a DE/WM, which writes the choice to a file. Then I use this file in .xinitrc to read the choice and start the DE/WM.
Here is the contents of session choice file:

#!/bin/bash

# Define the list of options
options=("wmaker" "plasma" "twm" "icewm" "lxqt" \
"fluxbox" "awesome" "openbox"  "default")

# Define the default choice (index of the options array, 0-based index)
default_choice=5

# Function to display the menu and read user input
choose_option() {
  echo "Please choose your desktop environment or window manager:"
  PS3="Enter your choice (1-${#options[@]}), or wait 1 seconds for the default: "

  select opt in "${options[@]}"; do
    if [[ -n "$opt" ]]; then
      echo "$opt" > ~/.selected_session
      echo "User selected: $opt"
      break
    fi
  done
}

# Run the menu in the background
choose_option &

# Get the PID of the background process
choose_option_pid=$!

# Wait for user input or timeout
if ! read -t 10 -p "Auto-selecting the default in 10 seconds... " user_input; then
  # Timeout happened
  echo "Timeout reached. Auto-selecting the default option."
  echo "${options[$default_choice]}" > ~/.selected_session
else
  # User input received
  if [[ $user_input -ge 1 && $user_input -le ${#options[@]} ]]; then
    echo "${options[$((user_input - 1))]}" > ~/.selected_session
    echo "User selected: ${options[$((user_input - 1))]}"
  else
    echo "Invalid input. Auto-selecting the default option."
    echo "${options[$default_choice]}" > ~/.selected_session
  fi
fi

# Kill the menu process if it is still running
kill $choose_option_pid 2>/dev/null

# Display the selected option for debugging purposes
selected_option=$(cat ~/.selected_session)
echo "Final selected option: $selected_option"

and here is the .xinitrc contents:

#!/bin/bash

# Set up .Xauthority
export XAUTHORITY=$HOME/.Xauthority

# Read the selected session from the file
if [[ -f ~/.selected_session ]]; then
  selected_choice=$(cat ~/.selected_session)
  #selected_choice="openbox"
else
  selected_choice=5  # Default to LXQt
fi

# Define the list of options again (index must match the select-session.sh)
options=("wmaker" "plasma" "twm" "icewm" "lxqt" \
"fluxbox" "awesome" "openbox"  "default")

selected_option="${options[$selected_choice]}"

# Output the selection for debugging purposes (can be removed in production)
echo "Selected option is: $selected_option"

# Common setup for X11 sessions
setup_x11() {
  export XDG_SESSION_TYPE=x11
  userresources=$HOME/.Xresources
  usermodmap=$HOME/.Xmodmap
  sysresources=/etc/X11/xinit/.Xresources
  sysmodmap=/etc/X11/xinit/.Xmodmap

  if [ -f $sysresources ]; then
      xrdb -merge $sysresources
  fi

  if [ -f $sysmodmap ]; then
      xmodmap $sysmodmap
  fi

  if [ -f "$userresources" ]; then
      xrdb -merge "$userresources"
  fi

  if [ -f "$usermodmap" ]; then
      xmodmap "$usermodmap"
  fi

  if [ -d /etc/X11/xinit/xinitrc.d ] ; then
    for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
      [ -x "$f" ] && . "$f"
    done
    unset f
  fi
}

# Launch the selected session
case "$selected_option" in
  "wmaker")
    setup_x11
    dbus-run-session wmaker
    ;;
  "plasma")
    setup_x11
    export DESKTOP_SESSION=plasma
    exec startplasma-x11
    ;;
  "twm")
    setup_x11
    twm &
    xclock -geometry 50x50-1+1 &
    xterm -geometry 80x50+494+51 &
    xterm -geometry 80x20+494-0 &
    dbus-run-session xterm -geometry 80x66+0+0 -name login
    ;;
  "icewm")
    setup_x11
    dbus-run-session icewm-session >~/.xinitrc.log 2>&1
    ;;
  "lxqt")
    setup_x11
    exec startlxqt	
    ;;
  "fluxbox")
    setup_x11
    dbus-run-session startfluxbox & wmpid=$!
    sleep 0.1
    wait $wmpid
    ;;
  "awesome")
    setup_x11
    dbus-run-session awesome
    ;;
  "openbox")
    setup_x11
    ~/.config/openbox/autostart
    dbus-run-session openbox
    ;;
  "default")
    setup_x11
    exec startlxqt
    ;;
  *)
    setup_x11
    exec $1
    ;;
esac

and I have this line in bash_profile:

#X11 Startx Automatically
[[ ! $DISPLAY && $XDG_VTNR -eq 1 ]] && ~/chooseDM && exec startx ~/.xinitrc

This it doesn't work and I cannot login to any session, no matter if I let the time pass to login to the default(lxqt) or choose another option. It just try to start X fails and then the script re-run at a loop. By testing the .xinitrc using the command:

startx ~/.xinitrc

I can see that it always try to start the 1st option which is the wmmaker, instead of what I chose or the default session.
Any help on this?

Last edited by zaxdan69 (2024-05-26 19:30:25)

Offline

#2 2024-05-26 19:33:33

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

Re: Help with auto-login script

That is obscenely over complicated.  Learn from this not to use chatGPT for such things.  Just put this all in your xinitrc:

# include boilerplate w/ xresources and the like here ... not in a function

[ -n "$1" ] && exec $1

wms="wmaker plasma twm icewm lxqt fluxbox awesome openbox"
echo $wms | tr ' ' '\n' | nl
read -sn 1 -t 10 -p "Enter a number: " index || index=5

case "$index" in
   1) exec wmaker ;;
   2)
      export DESKTOP_SESSION=plasma
      exec startplasma-x11
      ;;
   3)
      twm &
      xclock -geometry 50x50-1+1 &
      xterm -geometry 80x50+494+51 &
      xterm -geometry 80x20+494-0 &
      exec xterm -geometry 80x66+0+0 -name login
      ;;
   4) exec icewm-session ;;
   5) exec startlxqt ;;
   6) exec startfluxbox ;;
   7) exec awesome ;;
   8) exex openbox-session ;;
esac

Also note you should not use dbus-run-session at all, nor should you background then wait for fluxbox.

(edit: I filled in your existing options)

Last edited by Trilby (2024-05-26 19:56:31)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2024-05-26 20:05:53

zaxdan69
Member
Registered: 2016-06-04
Posts: 296

Re: Help with auto-login script

I tried that but it doesn't work. I don't get any prompt, X starts immediately and without any DM/WM, just a blank screen. I had the same problem before use a separate script for the selection.

Offline

#4 2024-05-26 20:57:27

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

Re: Help with auto-login script

EDIT: oops - it's been a while, perhaps xinitrc itself cannot have interactive components as the Xserver is already running.  The input would have to be done in your .profile:

# ...

if [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]]; then
   wms="wmaker plasma twm icewm lxqt fluxbox awesome openbox"
   echo $wms | tr ' ' '\n' | nl
   read -sn 1 -t 10 -p "Enter a number: " index || index=5
   exec startx "$index"
fi

Then in ~/.xinitrc:

# boilerplate stuff here...

case "$1" in
   1) exec wmaker ;;
   2)
      export DESKTOP_SESSION=plasma
      exec startplasma-x11
      ;;
   3)
      twm &
      xclock -geometry 50x50-1+1 &
      xterm -geometry 80x50+494+51 &
      xterm -geometry 80x20+494-0 &
      exec xterm -geometry 80x66+0+0 -name login
      ;;
   4) exec icewm-session ;;
   5) exec startlxqt ;;
   6) exec startfluxbox ;;
   7) exec awesome ;;
   8) exex openbox-session ;;
   *) exec $1 ;;
esac

Last edited by Trilby (2024-05-26 21:03:53)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2024-05-27 04:10:53

zaxdan69
Member
Registered: 2016-06-04
Posts: 296

Re: Help with auto-login script

No, this doesn't work either. Now the list appears, but the moment I press a key on keyboard starts the xserver with only the xterm in top left corner,  the same happens if I let the 10s pass without to press anything.
UPDATE:
I didn't use this approach but thanks to your comments, I thougt another way. So I'm using a separate script for choosing a DM/WM, and I edited .bash_profile, so to run the script instead of startx.
Then I modified the chatgpt's script to include the case statement with the DMs/WMs, write the contents of the choosed item to a file  and finally start the xserver with "startx ~/.xinitrc" command.
I removed the case statement from .xinitrc and I sourced the file which the script creates there.
This seems to work properly.

Last edited by zaxdan69 (2024-05-27 05:34:04)

Offline

#6 2024-05-27 07:11:16

seth
Member
Registered: 2012-09-03
Posts: 57,445

Re: Help with auto-login script

I don't think this is how startx handles parameters, it tries to execute $@ instead or on top of xinitrc.

As for the approach: how about you have a dmenu/rofi/xdialog/qarma (ie graphical X11) dialog that asks your for the desired session in your xinitrc?

Offline

#7 2024-05-27 08:59:39

zaxdan69
Member
Registered: 2016-06-04
Posts: 296

Re: Help with auto-login script

My goal is to have the ability to choose a different DM/WM if I want to but not all the time. In fact I wanted an auto-login to the default session and the ability to interrupt it if I press a key(esc or q) in 5 seconds for example. Like grub's menu function. The way I did it now is close but I need to have a longer timeout if I want to have time to read the items and choose one. So I have the timeout set to 10 seconds and an option to login manually to the default session by pressing enter without to input a number.
This is my current script:

#!/bin/bash

# Define the list of options
options=("wmaker" "plasma" "twm" "ede" "icewm" "lxqt" "pekwm" \
"fluxbox" "awesome" "qtile" "openbox" \
"cinnamon" "gnome" "xfce" "xmonad" \
"fvwm" "mate" "compiz" "cutefish" \
"budgie" "pantheon" "lumina" "enlightenment" \
"worm" "cde" "steamos" "trinity")

# Define the default choice (index of the options array, 0-based index)
default_choice=10

# Function to display the menu and read user input
choose_option() {
  echo "Please choose your desktop environment or window manager:"
  PS3="Enter your choice (1-${#options[@]}), or wait 10 seconds for the default: "

  select opt in "${options[@]}"; do
    if [[ -n "$opt" ]]; then
      selected_option="$opt"
      echo "User selected: $selected_option"
      break
    fi
  done
}

# Run the menu in the background
choose_option &

# Get the PID of the background process
choose_option_pid=$!

# Wait for user input or timeout
if ! read -t 10 -p "Auto-selecting the default in 10 seconds... " user_input; then
  # Timeout happened
  echo "Timeout reached. Auto-selecting the default option."
  selected_option="${options[$default_choice]}"
else
  # User input received
  if [[ $user_input -ge 1 && $user_input -le ${#options[@]} ]]; then
    selected_option="${options[$((user_input - 1))]}"
    echo "User selected: $selected_option"
  else
    echo "Invalid input. Auto-selecting the default option."
    selected_option="${options[$default_choice]}"
  fi
  fi

# Kill the menu process if it is still running
kill $choose_option_pid 2>/dev/null

# Display the selected option for debugging purposes
echo "Final selected option: $selected_option"

# Write the appropriate commands to ~/.selected_session
case "$selected_option" in
  "wmaker")
    echo -e "exec wmaker" > ~/.selected_session
    ;;
  "plasma")
    echo -e "export DESKTOP_SESSION=plasma\nexec startplasma-x11" > ~/.selected_session
    ;;
  "twm")
    echo -e "twm &\nxclock -geometry 50x50-1+1 &\nxterm -geometry 80x50+494+51 &\nxterm -geometry 80x20+494-0 &\ndbus-run-session xterm -geometry 80x66+0+0 -name login" > ~/.selected_session
    ;;
  "ede")
    echo -e "exec startede" > ~/.selected_session
    ;;
    "icewm")
    echo -e "exec icewm-session >~/.xinitrc.log 2>&1" > ~/.selected_session
    ;;
  "lxqt")
    echo -e "exec startlxqt" > ~/.selected_session
    ;;
  "pekwm")
    echo -e "exec pekwm" > ~/.selected_session
    ;;
  "fluxbox")
    echo -e "exec startfluxbox & wmpid=\$!\nsleep 0.1\nwait \$wmpid" > ~/.selected_session
    ;;
  "awesome")
    echo -e "exec awesome" > ~/.selected_session
    ;;
  "qtile")
    echo -e "exec /usr/bin/qtile start\nexec xtrace qtile >> ~/qtile.log" > ~/.selected_session
    ;;
  "openbox")
    echo -e "~/.config/openbox/autostart\ndbus-run-session openbox" > ~/.selected_session
    ;;
  "cinnamon")
    echo -e "exec cinnamon-session" > ~/.selected_session
    ;;
    "gnome")
    echo -e "exec gnome-session" > ~/.selected_session
    ;;
  "xfce")
    echo -e "exec xfce4-session" > ~/.selected_session
    ;;
  "xmonad")
    echo -e "xrdb -merge .Xresources\nxpmroot ~/background.xpm &\ndbus-run-session xmonad" > ~/.selected_session
    ;;
  "fvwm")
    echo -e "exec fvwm" > ~/.selected_session
    ;;
  "mate")
    echo -e "exec mate-session" > ~/.selected_session
    ;;
  "compiz")
    echo -e "/home/zaxus/.config/compiz-1/compiz-session" > ~/.selected_session
    ;;
  "cutefish")
    echo -e "exec cutefish-session" > ~/.selected_session
    ;;
  "budgie")
    echo -e "export XDG_CURRENT_DESKTOP=Budgie:GNOME\nexec budgie-desktop" > ~/.selected_session
     ;;
  "pantheon")
    echo -e "io.elementary.wingpanel &\nplank &\ndbus-run-session gala" > ~/.selected_session
    ;;
  "lumina")
    echo -e "exec start-lumina-desktop" > ~/.selected_session
    ;;
  "enlightenment")
    echo -e "exec /usr/bin/enlightenment_start" > ~/.selected_session
    ;;
  "worm")
    echo -e "exec worm" > ~/.selected_session
    ;;
  "cde")
    echo -e "export PATH=\$PATH:/usr/dt/bin\nexport LANG=C\ndbus-run-session /usr/dt/bin/Xsession" > ~/.selected_session
    ;;
  "steamos")
    echo -e "exec steamos-session" > ~/.selected_session
    ;;
  "trinity")
    echo -e "exec /opt/trinity/bin/starttde" > ~/.selected_session
    ;;
  enter)
    echo -e "exec startlxqt" > ~/.selected_session
    ;;
esac

startx ~/.xinitrc

I don't have all those installed(I've test them in the past though), but I keep the options there so to be ready.

Offline

#8 2024-05-27 11:20:03

seth
Member
Registered: 2012-09-03
Posts: 57,445

Re: Help with auto-login script

ret="$(printf 'foo\nbar\nbaz' | dmenu & D_PID=$!; sleep 5; kill $D_PID)"
echo $ret

Offline

#9 2024-05-27 11:59:37

zaxdan69
Member
Registered: 2016-06-04
Posts: 296

Re: Help with auto-login script

What is this? I don't think that it will work on tty. I haven't use dmenu but I think that is a launcher which needs an xsession to run. 
P.S: I tried another approach using dialog and I'm very close to what I want to achive.

Offline

#10 2024-05-27 12:06:03

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

Re: Help with auto-login script

Seth's suggestion is not meant to run from a tty, but from the start of ~/.xinitrc.

Last edited by Trilby (2024-05-27 12:07:39)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2024-05-27 12:09:37

seth
Member
Registered: 2012-09-03
Posts: 57,445

Re: Help with auto-login script

https://man.archlinux.org/man/extra/dmenu/dmenu.1.en is not "a launcher" but a general purpose menu thing for X11 and this is to illustrate how you can go about running a GUI menu w/ an auto-timeout in your xinitrc to decide what session you're gonna start. You don't have to ask that question in the TTY if you're gonna start an X11 server anyway.

(Though the approach is crude because it'll timeout after 5s whether you started typing or not)

Offline

#12 2024-05-27 12:20:30

zaxdan69
Member
Registered: 2016-06-04
Posts: 296

Re: Help with auto-login script

Okay thanks. I'll look at it.

Offline

#13 2024-05-30 00:56:00

solskog
Member
Registered: 2020-09-05
Posts: 429

Re: Help with auto-login script

zaxdan69 wrote:

the moment I press a key on keyboard starts the xserver with only the xterm in top left corner

Triby's solution is working, besides one thing, the /bin/startx uses a "defaultclient=xterm" as the first argument. For ~/.xinitrc you need to check the second argument instead.

# cat ~/.xinitrc
case "$2" in
   1) exec wmaker ;;
   2)
...
# exec startx wmaker

Last edited by solskog (2024-05-30 01:08:50)

Offline

#14 2024-05-30 01:25:21

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

Re: Help with auto-login script

solskog wrote:

... the /bin/startx uses a "defaultclient=xterm" as the first argument.

Wow - is that new?  I've steered clear of using startx itself for as long as I can remember as it's always been a complete mess of a script (and is completely unnecessary) but adding a parameter is really odd.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#15 2024-05-30 07:28:41

solskog
Member
Registered: 2020-09-05
Posts: 429

Re: Help with auto-login script

Trilby wrote:

but adding a parameter is really odd.

xorg-xinit 1.4.2-1
The first argument to /bin/startx will always be the "clientargs" by the code itself.

# cat /bin/startx
whoseargs="client"
while [ x"$1" != x ]; do
    case "$1" in
    # '' required to prevent cpp from treating "/*" as a C comment.
    /''*|\./''*)
 if [ "$whoseargs" = "client" ]; then
     if [ x"$client" = x ] && [ x"$clientargs" = x ]; then
  client="$1"
     else
  clientargs="$clientargs $1"
     fi

Which leads to:

# exec /bin/startx openbox
...
+ whoseargs=client
+ '[' xopenbox '!=' x ']'
+ case "$1" in
+ '[' client = client ']'
+ clientargs=' openbox'
+ shift
... 
+ xinit xterm openbox -- /etc/X11/xinit/xserverrc :0 vt1 -keeptty -auth /tmp/serverauth.ybPUK2hy3L

Therefore ~/.xinitrc needs to check the second argument. It also looks odd to me.

Offline

Board footer

Powered by FluxBB