Shell script statements not running one after one - linux

I wrote a shell script that calls some other shell scripts in a new terminal window. It was working fine on my computer. its like this
#!/bin/sh
gnome-terminal -e "sh one.sh"
zenity --info --text "exed one"
gnome-terminal -e "sh 2.sh"
zenity --info --text "exed 2"
firefox "www.aurl1.com" "www.aurl2.com"
According what I understood, the script will first open a terminal and run the first script, after finishing that it will show the zenity then run two in new terminal then show zenity like that. But when the same was exed on another computer, the script is not following this order. It simply opens all terminal side by side not waiting one to finish and showing the dialogues together. Why is this problem ? Thanks in advance.

It used to be that gnome-terminal would run each terminal in its own process. But now gnome-terminal checks on startup if there is already a gnome-terminal process running, and if so, tells the existing terminal process to open a new window with the given arguments. There is no problem with your script: it is waiting for each process to finish in turn, it is just that gnome-terminal is exiting immediately after passing off its work to a different process.
According to man gnome-terminal, you can use the --disable-factory option to disable this behaviour, and run each command in its own process.
Try this:
gnome-terminal --disable-factory -e 'sh -c "echo hi && sleep 10"'
echo now you can run script 2

Run them like this:
gnome-terminal -e "sh one.sh" ; zenity --info --text "exed one" ; gnome-terminal -e "sh 2.sh" ; zenity --info --text "exed 2"
What the comma does is it tells the shell to execute the first command, wait for it to finish and only then move on to the next. Otherwise, the behavior you described is normal, it parses the lines one by one, without waiting for anything.

Related

how to create and move between screens in a bash.sh script [duplicate]

Was wondering how I can start up a command such as:
while :; do ./myCommand; done;
But instead of doing the usual
screen -S nameOfMyScreen
Then the command
while :; do ./myCommand; done;
Then detach the screen
^a ^d (Control "a" the control "d"
I would like it to start and detach. Thanks!
screen -d -m sh -c "while :; do ./myCommand; done;"
Explanation:
-d -m starts screen in detached mode (create session but don't attach to it)
sh -c commandline starts a shell which executes the given command line (necessary, since you are using the while builtin).
From screen -h, these look useful:
-dmS name Start as daemon: Screen session in detached mode.
-X Execute <cmd> as a screen command in the specified session.
I haven't done this myself, but that's where I'd start.
Update:
The top of the help also says
Use: path/to/screen [-opts] [cmd [args]]
so the -X switch may be to execute a screen command as opposed to a shell command. You might just be able to put your command after the -dmS <name> without any -X switch.

How can I automatically record the terminal session every time a new terminal window is opened?

I have a script I would like to run when a terminal is opened. This will use the script command to record the terminal session, to aid in troubleshooting the system after some software work is done. When I placed the script in .bash.d, and opened a terminal, it gets stuck in a loop repeatedly starting the script.
script -a -f ~/user_data/error_logs/terminal_output/typescript.$(hostname).$(date -I).txt
I was able to fix it by placing the following in the .bashrc
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (/data/file_upload/term_log.sh)
The contents of term_log.sh are:
script -a -f -q ~/user_data/error_logs/terminal_output/typescript.txt
kill -9 $PPID
I added the kill -9 $PPID so that when a user typed exit it would close the terminal and not just quit the script.
I have never used bash.d. But I put commands I would like to run at the start of terminal or more specifically bash in ~/.bash_profile and it works like charm.
You will have to close the bash session and start a fresh session for the command you put in ~/.bash_profile to take effect, as commands in ~/.bash_profile are executed only when the bash session starts. Alternatively, after you have put the command in ~/.bash_profile, you can run source ~/.bash_profile for the command to take effect.

Execute bash after terminating webpack-dev-server

Here's a small annoyance. As part of a project's "quickstart" script I'm starting a webpack server in a terminal tab, along other things in other tabs, in short:
#!/usr/bin/env bash
gnome-terminal --tab --tab --command \
'bash -c "node_modules/.bin/webpack-dev-server; exec bash"' &
This almost works as supposed to -- with the exception of Ctrl+C in the server's tab. If it were, say, a Flask server, it would get stopped and a bash prompt would appear in the same tab (that's the reason for the "exec bash" part). But with node / webpack the tab just closes.
Manually sending an interrupt signal to the node process leaves the tab open (e.g. kill -INT <pid>). So the question is what is happening from the operating system perspective. What process tree is created? Which process gets the SIGINT? What gets replaced by exec (if anything)?
Possibly related: https://github.com/nodejs/node/issues/4432.
try to use trap as follow:
bash -c "trap 'exec bash' SIGINT; node_modules/.bin/webpack-dev-server;"
it must kill webpack-dev-server and exec bash on cmd+c;

xterm doesn't run shell script

so I'm trying to use this command:
export DISPLAY=:1; /usr/bin/xterm -hold -e /path/to/shscript
Where shscript is:
#!/bin/bash
echo "Restarting ... $(date)" >> /var/log/mw2.txt
if screen -ls | grep -q 'test'; then
screen -X -S test quit
sleep 1000
screen -d -m -S test wine iw4m.exe -dedicated -console +dw_licensefile license.dat +set net_port "28960" +set party_maxplayers 18 +exec server.cfg +map_rotate +set fs_game "mods/tsd"
else
screen -d -m -S test wine iw4m.exe -dedicated -console +dw_licensefile license.dat +set net_port "28960" +set party_maxplayers 18 +exec server.cfg +map_rotate +set fs_game "mods/tsd"
fi
So I want to launch a new xterm window and it to run a shell script.
But whatever shell script I try to use, it doesn't run it. It just goes blank. All other commands work, but when I put a shell script in it, it just goes blank and does nothing. I can't find a solution for this, please help, thank you.
I get this with set -x:
++ date
+ echo 'Restarting ... (date)'
+ grep -q test
+ screen -ls
+ screen -d -m -S test -wine iw4m etc...
I think you get exactly what you have asked for...
From screen's man page:
-d -m Start screen in "detached" mode. This creates a new session but
doesn't attach to it. This is useful for system startup
scripts.
On the other hand you use -hold for xterm:
-hold Turn on the hold resource, i.e., xterm will not immediately
destroy its window when the shell command completes. It will
wait until you use the window manager to destroy/kill the win‐
dow, or if you use the menu entries that send a signal, e.g.,
HUP or KILL.
And that's exactly what you see. xterm starts, executes screen which runs, but you don't see the output as screen does not attach to the virtual terminal. Since screen has detached the shell script exits and xterm holds the window for you to destroy it at your leisure.
I bet that if in another xterm you attach to the session with screen -S test you'll see the output.
Yep. It was the wine command failing. I changed some stuff, and it works now. I had to put cd /home/MW2 in the script for it to be able to run the iw4m.exe, then screen -S etc.

Run a shell script in new terminal from current terminal

How do you run a shell script in a new terminal in Linux from a terminal like "start test.bat" in Windows, also it should be working in the console mode.
Here's a simple example to get you started:
To write a shell script, do this on your command prompt:
echo -e '#!/bin/sh\n echo "hello world"' > abc.sh
This writes:
#!/bin/sh
echo "hello world"
To a file called abc.sh
Next, you want to set it to executable by:
chmod +x abc.sh
Now, you can run it by:
./abc.sh
And you should see:
hello world
On your terminal.
To run it in a new terminal, you can do:
gnome-terminal -x ./abc.sh
or, if it's xterm:
xterm -e ./abc.sh
Here's a list of different terminal emulators.
Alternatively, you just run it in your current terminal, but background it instead by:
./abc.sh &
I came here wanting to figure out how to make a script spawn a terminal and run it self in it, so for those who want to do that I figured out this solution:
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window with same arguments
x-terminal-emulator -e "$0" "$#"
# and abort running the rest of it
exit 0
fi
For gnome try this.
Replace ls with the command you want to run
gnome-terminal -x sh -c "ls|less"
I hope this is what you want
As of January 2020, the -e and -x option in gnome-terminal still run properly but throw out the following warnings:
For -e:
# Option “-e” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
For -x:
# Option “-x” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh
I hope this helps anyone else presently having this issue :)

Resources