WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved]: why doesn't startx work from within a shell script?  (Read 5458 times)

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #15 on: November 11, 2022, 10:52:28 AM »
The script runs as regular user, which is bruno in my case (I use the user=bruno bootcode).

I don't have a problem per se. Just trying to solve the mystery of why this particular script doesn't work as expected unless it is launched while X is not running. I don't like mysteries of this kind.
« Last Edit: November 11, 2022, 10:55:19 AM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11226
Re: why doesn't startx work from within a shell script?
« Reply #16 on: November 11, 2022, 10:54:40 AM »
Hi GNUser
I believe  $HOME/.profile  gets run when that user gets logged in.

Startx is is just a script that looks at and sets up a bunch of variables.
Code: [Select]
less -I `which startx`
It exits if it can't locate a desktop to use.

At the end, it runs:
Code: [Select]
exec $HOME/.xsessionWhich starts Xorg/Xvesa, loads the desktop, and runs the contents of  $HOME/.X.d.


Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #17 on: November 11, 2022, 11:00:10 AM »
Thanks, Rich. I tried changing startx to exec $HOME/.xsession and script still only works if launched when X is not running. Very strange.

I'm sorry I bothered you guys with this. I guess this one will remain a mystery.

Online patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 674
Re: why doesn't startx work from within a shell script?
« Reply #18 on: November 11, 2022, 11:02:49 AM »
Thx @Rich, for your support.
OK, Bruno, with the boot code user=bruno, i think there some of the boot scripts runs as root because they need to do so.
So you have to se what user runs your script ??
You can verify that with like i said in previous thread.
You have to add one more line to your script file :)

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #19 on: November 11, 2022, 11:46:16 AM »
Maybe the issue is that startx has to be launched from an interactive login shell in order to work. If I ever figure it out I'll post something here.

Thanks guys, but I'm giving up on this for now.

Offline gadget42

  • Hero Member
  • *****
  • Posts: 663
Re: why doesn't startx work from within a shell script?
« Reply #20 on: November 12, 2022, 03:32:22 AM »
i used @Rich "less -I `which startx`" on devuan beowulf and compared it to a CorePlus-13x LiveCD booted into just the basic GUI startx and the devuan beowulf is different/larger/more-commented(naturally)

see attached file for startx from devuan beowulf(NOTE:no-systemd/non-systemd/whichever-you-prefer!)

in this version of startx there is a link to:
https://bugzilla.redhat.com/show_bug.cgi?id=806491

and i also did a search using google with a cut-and-paste from the beginning comment of the devuan beowulf startx script:
Code: [Select]
https://www.google.com/search?q=%23+This+is+just+a+sample+implementation+of+a+slightly+less+primitive+%23+interface+than+xinit.+It+looks+for+user+.xinitrc+and+.xserverrc+%23+files%2C+then+system+xinitrc+and+xserverrc+files%2C+else+lets+xinit+choose+%23+its+default.+The+system+xinitrc+should+probably+do+things+like+check+%23+for+.Xresources+files+and+merge+them+in%2C+start+up+a+window+manager%2C+%23+and+pop+a+clock+and+several+xterms.&oq=&aqs=
with many results(naturally)...so scrolled through results on first page and went to:
https://forums.bunsenlabs.org/viewtopic.php?id=4647

which may or may-not be of assistance/help/etc

PLEASE NOTE: both provided URLs link to distros that ARE currently using SYSTEMD(just-an-fyi/reminder/footnote)

also note that "ls -ahlrt" while working in your home directory(tcl-default=/home/tc) shows output in reverse-order-based-on-time(so newest/last-written-to/etc at the bottom of the list)

20221112-0538am-cdt-usa-modified: just wanted to add that that redhat bug report was created/started in 2012...so this is NOT a new bug/curiosity/mystery/question...
« Last Edit: November 12, 2022, 03:38:41 AM by gadget42 »
The fluctuation theorem has long been known for a sudden switch of the Hamiltonian of a classical system Z54 . For a quantum system with a Hamiltonian changing from... https://forum.tinycorelinux.net/index.php/topic,25972.msg166580.html#msg166580

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #21 on: November 15, 2022, 05:29:57 PM »
Hi, Rich. I know you are a wizard. If you have the time and interest, can you help me tweak this simple script so that it works?
Code: [Select]
#!/bin/sh
pkill Xorg
sleep 3
startx
Expected behavior after launching this script from a terminal emulator (e.g., aterm or xterm) is this:
1. user's desktop disappears
2. console shows up for 3 seconds
3. user's desktop reappears
Actual behavior is this:
1. user's desktop disappears
2. console shows up
3. user's desktop does not reappear (user must type startx at the console's command prompt for desktop to reappear)

I'm asking for help because I've already scoured the internet and tried too many things to enumerate.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11226
Re: why doesn't startx work from within a shell script?
« Reply #22 on: November 15, 2022, 10:24:12 PM »
Hi GNUser
I got this to work. I created a script called  RestartX:
Code: [Select]
#!/bin/sh
pkill Xorg
sleep 3
sudo openvt -c 1 -s login -f tc

Then launched it like this:
Code: [Select]
./RestartX &

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10963
Re: why doesn't startx work from within a shell script?
« Reply #23 on: November 15, 2022, 11:27:10 PM »
When xterm dies, it may take the script with it. That kills the terminal it was running in, and it has no more input or output. nohup is used to work around that, but it'd be better to launch it from somewhere else.
The only barriers that can stop you are the ones you create yourself.

Offline gadget42

  • Hero Member
  • *****
  • Posts: 663
Re: why doesn't startx work from within a shell script?
« Reply #24 on: November 16, 2022, 12:17:04 AM »
did a little browsing and found this thread interesting(perhaps moreso for our future readers):

https://stackoverflow.com/questions/15595374/whats-the-difference-between-nohup-and-ampersand

another link for reference(but one of the answers/replies refers back to the above thread):

https://stackoverflow.com/questions/40614243/nohup-does-not-work-properly

20221116-0222am-cdt-usa-modified-added additional link
« Last Edit: November 16, 2022, 12:22:19 AM by gadget42 »
The fluctuation theorem has long been known for a sudden switch of the Hamiltonian of a classical system Z54 . For a quantum system with a Hamiltonian changing from... https://forum.tinycorelinux.net/index.php/topic,25972.msg166580.html#msg166580

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #25 on: November 16, 2022, 06:56:55 AM »
Thank you, Rich. Wow, it works exactly as expected.

For those of you who don't know what Rich looks like in person, here is a recent portrait:



« Last Edit: November 16, 2022, 07:13:11 AM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11226
Re: why doesn't startx work from within a shell script?
« Reply #26 on: November 16, 2022, 07:32:00 AM »
Hi curaga
When xterm dies, it may take the script with it. ...
The script can be launched from a terminal or through .X.d.
I launched the following test script in the background from pts/7:
Code: [Select]
#!/bin/sh

while true
do
sleep 1
pgrep Xorg > /dev/null || break
done
As long as pgrep finds Xorg, it will keep on running.

Then from another terminal:
Code: [Select]
tc@E310:~$ tty
/dev/pts/9
tc@E310:~$ ps -Af | grep './test' | grep -v grep
tc       10517  9258  0 09:59 pts/7    00:00:00 /bin/sh ./test
The program is running on pts/7 which has a PID of 9258.

Code: [Select]
tc@E310:~$ sudo kill -9 9258
tc@E310:~$ ps -Af | grep './test' | grep -v grep
tc       10517     1  0 09:59 ?        00:00:00 /bin/sh ./test
tc@E310:~$
The terminal was killed, but the script was just re-parented to PID 1
with no tty listed.

If tty1 dies, it should respawn via inittab and present a login prompt
on the console.

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #27 on: November 16, 2022, 09:03:56 AM »
Hi, Rich. When your script restarts X, it leaves this message in the console: "-sh: can't access tty; job control turned off". I don't think there is a real problem because everything works fine within the restarted X session.

Do you know what the message means? And how do we make it not show up? I tried adding 2>/dev/null to various places but the message still shows up.
« Last Edit: November 16, 2022, 09:14:46 AM by GNUser »

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1347
Re: why doesn't startx work from within a shell script?
« Reply #28 on: November 16, 2022, 09:50:22 AM »
Hello again, Rich. If you have any insights about the error message, great. If not, no worries. You've already helped me enough with this.

Topic may be marked as Solved :)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11226
Re: [Solved]: why doesn't startx work from within a shell script?
« Reply #29 on: November 16, 2022, 11:39:58 AM »
Hi GNUser
I had the same message showing up. I only did a brief search
on that, so short answer suggests consoles do not handle job
control and the message is just informational.

... Here is my  x-watchdog  script:

Code: [Select]
#!/bin/sh

log=$HOME/log.txt

while true; do
if pgrep Xorg >/dev/null; then
true
else
echo "Restarting X" >>$log
startx
fi
sleep 10
done
...
The second  true  statement doesn't do anything.  while true  will run
forever unless a  break  statement is encountered.

Just a little simplification:
Code: [Select]
#!/bin/sh

log=$HOME/log.txt

while true
do
sleep 10
# This loops every 10 seconds as long as Xorg is found.
pgrep Xorg > /dev/null && continue
# We drop through to here when Xorg is not found.
echo "Restarting X" >>$log
sudo openvt -c 1 -s login -f tc
done
Or in your case, I suspect the user is  bruno , not  tc.