Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help from a fellow Linux Jedi --- SOLVED! --- NOT YET.
#1
I have a script that runs in the background and occasionally calls xterm and installs something. this works,

my problem is that sometimes it needs to call xterm more than once, and depends on the first command to finish. but it doesn't wait.



Code:
#!/bin/bash

xterm -e(or --) sudo apt update
xterm -e sudo apt install -y whatever_app

# NOT the actual code.,  the same basic layout but with terminator

I have forgotten how to get the second command to wait for the first one to finish


I know the solution is very simple,,,,, for the life of me i can not remember. I tried a Google search, but nothing useful.



any advice is very appreciated.



kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#2
Elias,

Doesn't this '&' tell the script to wait for the code that it follows to finish before running the next line of code?

Richard
Reply
#3
Not quite, Richard. A single '&' tells a task to run in the background. (Daemon)

I guess, eliasw4u is looking for '&&'. This tells the second command to only execute, when the first command finished successfully. (Boolean AND)

HTH
Reply
#4
I have tried & and && (that looks strange when reading it) but neither worked.

EDIT
------------------------------------------------------------
the fail was with terminator. so i tried it actually using xterm and it worked but it was buggy. i switched it to Gnome-terminal and changed the " -e " to " --  " and it worked perfectly.


a simple example
Code:
gnome-terminal -- sudo apt remove vlc && gnome-terminal -- sudo apt install vlc
I had originally tried that and in terminator, but it didn't work. so i assumed it was the wrong code, and that i had forgotten again. it never occurred to me that the problem was the emulator i was using. thanks for the info. I was able to find the solution by getting conformation that "&&" was correct. so thank you for that.

problem SOLVED! ALMOST.
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#5
ok so with the given string it does work but with this it does not.
Code:
#!/bin/bash

gnome-terminal -- sudo apt-get update && gnome-terminal -- sudo /location/of/my/script/script.sh
it opens both terminals at the same time.  with xterm, if i tell it to sleep between the two commands
Code:
xterm -e sudo apt-get update && sleep 3 && xterm -e sudo /location/of/my/script/script.sh



it seems to work ok. but i would prefer to use terminator or gnome-terminal. but both of them try and run the two commands at the same time.

can anyone explain why? and how to fix this? if need be i will use xterm, but,,,,,,


again, thanks for the quick replies.


kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#6
I'm pretty sure, your problem is that you start two instances of the terminal. The '&&' doesn't check if the first command inside the terminal finished successfully, but rather if the first instance of the terminal itself started correctly, which should almost always be the case. Sorry that I ignored this little tidbit in my first answer.

Is it really necessary to start two terminal instances?

Why not simply create a script with the wanted commands, like so
Code:
#!/bin/bash
sudo apt-get update &&
sudo /location/of/my/script/script.sh
and save it to /location/of/new/script/newscript.sh.

You could then execute this newly created script in your terminal.
Code:
terminal emulator of choice -e /location/of/new/script/newscript.sh

Just a thought.
Reply
#7
Have you tried using the wait command?

command1
wait
command2
Jeremy (Mr. Server)

* Desktop: Ubuntu MATE
* Windows are for your walls, Apple is for your health, Linux is for your computer
Reply
#8
As radolkin stated the xterm or gnome-terminal exit code is not related to the command exit code being viewed by it's window.


The xterm program has a xterm process to xterm window one to one relationship.

$ xterm 

When you run the xterm program, it will create a new window running the default bash shell.  Your original command line blocks until the new xterm process exits.  The new xterm process will exit when you click the new xterm window and exit the interactive bash shell. The exit code of the shell commands are independent of the xterm exit code.


The gnome-terminal program does not behave like the traditional xterm. The gnome-terminal program will use the D-Bus (Desktop Bus) as an ipc to notify the gnome-terminal-server process to establish a new window. This is a client/server like model. After the gnome-terminal program initiates the server request the process exits.  

$ gnome-terminal
$ echo $?
0
$
$ ps -ef | grep gnome-terminal
user  5688  1746  1 01:56 ?        00:00:00 /usr/lib/gnome-terminal/gnome-terminal-server
user  5709  3417  0 01:56 pts/0    00:00:00 grep --color=auto gnome-terminal


In the above example, the gnome-terminal program without options will initiate a spawn of the new gnome-terminal window running the default bash shell.  While the gnome-terminal-server process facilitates the new gnome-terminal window, the original gnome-terminal program terminates with an exit code of 0. The subsequent "ps -ef |grep gnome-terminal" command only reveals the gnome-terminal-server process.  Remember after the gnome-terminal client program initiates the server process to spawn a new window, it will terminate with a 0 exit code.


One xterm process using one xterm window:

xterm -e bash -c "id && pwd ; read -p \"Enter to close.\""


Two xterm processes using two xterm windows:

xterm -e bash -c "pwd && xterm -e bash -c 'id ;read -p \"Enter to close.\"'"



One gnome-terminal client program initiating one gnome-terminal-server window:

gnome-terminal -- bash -c 'id && pwd ; read -p "Enter to close."'


Two gnome-terminal client programs initiating two gnome-terminal-server windows:

gnome-terminal -- bash -c "id && gnome-terminal -- bash -c 'pwd ;read -p \"Enter to close.\"' ; read -p \"Enter to close.\" "


*  To keep the windows open for testing, the read -p "Enter to close." statements were used.


Edit: 2020525_1914EDT The last gnome-terminal command was using a debug syntax with the "false" statement.  It was changed with the proper syntax.
.
Idea Give a person a fish, and you feed them for a day. Teach a person how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
Reply
#9
(05-21-2020, 03:11 PM)cleverwise Wrote: Have you tried using the wait command?

command1
wait
command2
yes. and it didn't have any affect as far as waiting for the previous terminal command to exit.

though as i said above, if i use xterm and add a sleep command (amount of time irrelevant) between the commands, it works without and bugs. But fails with any other terminal emulator i have tried.

for now i can use  xterm, but i would rather be able to use terminator or gnome-terminal

kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#10
(05-25-2020, 08:47 PM)deck_luck Wrote: As radolkin stated the xterm or gnome-terminal exit code is not related to the command exit code being viewed by it's window.

.............

One gnome-terminal client program initiating one gnome-terminal-server window:

gnome-terminal -- bash -c 'id && pwd ; read -p "Enter to close."'


Two gnome-terminal client programs initiating two gnome-terminal-server windows:

gnome-terminal -- bash -c "id && gnome-terminal -- bash -c 'pwd ;read -p \"Enter to close.\"' ; read -p \"Enter to close.\" "

...........

The "Two gnome-terminal client" is  almost exactly what i was looking for. as soon as i saw the line of code it was clear. But where did you find this gem? I have searched the internet for over two weeks, and nothing.

back in 2012 i had a script that called the XFCE terminal with something similar, but I don't have that script anymore unfortunately. it was replaced with a more strait-forward approach. It is this that I am ultimately searching for.

However this Gem will work as a patch until i can find the other. the code i plan on using here will basically be like this
Code:
#!/bin/bash

gnome-terminal -- bash -c "sudo apt update && gnome-terminal -- bash -c '/home/user/path/to/script.sh  ;read -p \"Enter to close.\"' "
It will be within a script that runs automatically whenever someone connects to my server. This will ensure that there system is up to date, and compatible with my security protocols.

The reason for this is because 3 weeks ago someone logged into my server with an old knoppix live system. from 2013. and was able to bypass certain security protocols. it was one of my neighbors kids. no harm was done. But I now know that I need to add an extra layer of security. this will force there system to be updated. and force them to enter one of MY passwords. without it, no bypass. no access.

I will expand on it to include all other known package managers. But you get the basic idea.

also, if there is a better way to do that,,, i am open for suggestions.

so ya, thanks for that deck_luck. 

kudos

P.S.
No, I do not know how the kid did it. But I plan on finding out.

wow there are a few things wrong with what i just posted. the most obvious, what if they don't have that terminal emulator installed.

i will work out the kinks. that isn't to hard of a work around from this point.
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)