Using of gnome terminal to get new tab - linux

I'm new to scripting.
I’m using the below code in my project, but the terminal is getting closed instantly. I want the hold the terminal and keep running
#!/bin/bash/
gnome-terminal --tab --working-directory="/home/sandhya/OpenBTS/public/openbts/trunk/apps/" -e "file = grep OpenBTS /home/sandhya/OpenBTS/public/openbts/trunk/apps \
if [ -f $file ] \
then \
echo " file exits" \
sudo "/home/sandhya/OpenBTS/public/openbts/trunk/apps/OpenBTS" \
fi"
I want the terminal to be stay and keep running the application.
Please correct me in case the method or syntax I used is wrong.

In gnome-terminal,
--go to Edit -> Profiles.
--Double click on Default profile
--Click on Title And Command tab.
--Select Hold Terminal Opens from when command exits list which located at bottom of window.
--close windows
Now run your script it will work
Or you can also add your profile and run script as follows
Click the New tab. and give your profile name
Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
To make the terminal stay when the command exits:
In konsole there is a --noclose flag.
In xterm, there is a -hold flag.

Related

How to choose a default tab focus in gnome-terminal?

I have a bash script which opens several tabs inside one window and I want to choose one tabe to be the default focus
for example the below script opens 3 tabs on one window:
gnome-terminal --tab --geometry="100x20" --title="TAB1" -- bash -ic "command1"
gnome-terminal --tab --geometry="100x20" --title="TAB2" -- bash -ic "command2"
gnome-terminal --tab --geometry="100x20" --title="TAB3" -- bash -ic "command3"
So, let's say I want tab2 to be the focus after running the script and openning the window. Is there a way to specify that from the script?
I highly recommend installing terminator, it's a program which offers flexible management of multiple running gnome terminals. It allows you to choose a tab as the default focus while still viewing any other amount of terminals on the same view. It can be installed via:
$ sudo apt install terminator

Opening new gnome-terminal (v3.28+) with multiple tabs and different commands

It seems the behavior of gnome-terminal has changed between the version shipped with Ubuntu 14 (v3.6?) and Ubuntu 18 (v3.28).
I have a script that opens a new gnome-terminal with a bunch of tabs setup to different directories for my development, and currently the first tab runs a script. The command to open the gnome-terminal with tabs looks something like this:
gnome-terminal \
--tab --command="myscript.sh" \
--tab --working-directory="<some dir 1>" \
--tab --working-directory="<some dir 2>" \
...
This works perfectly as desired in the gnome-terminal version that shipped with Ubuntu 14 (v3.6?).
But in the gnome-terminal version that ships with Ubuntu 18 (v3.28) several things have changed:
Unless I add the --window option, the tabs open in the current gnome-terminal, not a new one. Unfortunately adding the --window option opens an initial blank tab. Is it possible to open a new window with only the tabs that I specify?
I now get the following notice (though it functions as before):
# Option “--command” 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.
Changing my script per this guidance changes the behavior such that the command is issued to all tabs, whereas before I could apply a unique command to each tab. Does this mean the ability to run a separate command per tab has been deprecated, or am I missing something?
I appreciate suggestions on how to change my script to support the old behavior in the newer gnome-terminal.
1) Use --window for your first tab
gnome-terminal \
--window -t 'Tab 1' \
--tab -t 'Tab2' --working-directory="<some dir 1>" \
--tab -t 'Tab3' --working-directory="<some dir 2>" \
...
Unfortunately this will only allow one command to be passed in using the new design, and the window/tabs close at completion (I'm not sure if that was the behavior before)
2) If you don't care about the tab closing when the command is complete, you could do this:
$ gnome-terminal --window -- ./mytabs.sh
mytabs.sh
#!/bin/bash
gnome-terminal --tab -t 'Tab 1' -- ./myscript.sh
gnome-terminal --tab -t 'Tab 2' --working-directory="<some dir 1>"
gnome-terminal --tab -t 'Tab 3' --working-directory="<some dir 2>"
This will open each tab from the script in the window that was created in the code above it. It's a pain in that you either have to type out the first command or create a second script.

How to execute multiple statements in multiple terminals? (Linux, Bash)

I'm trying to write a script that opens 3 terminal windows and runs a couple of statements in those windows. But it's not working. I've tried using the && operator as well as " " but I can't get it to work. I've also tried it with the statements on the same line as well as below each other. The error I'm receiving is that the cd child process failed to execute stating that there is no such Directory. But the directory ~/Projects/catkin_ws is correct.
#!/bin/bash
# ROS opstarten
gnome-terminal -e cd ~/Projects/catkin_ws source devel/setup.bash roscore
# gazebo opstarten
gnome-terminal -e cd ~/Projects/catkin_ws
source devel/setup.bash
roslaunch cvg_sim_gazebo Qr_Chessboard.launch
# programma opstarten
gnome-terminal -e cd ~/Projects/catkin_ws
source devel/setup.bash
/usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py
If you really want to run them on separate terminals programmatically you can use a terminal multiplexer such as GNU screen for that.
First you have to start a session:
$ screen -S demo
Then open all the terminals you need inside it with Ctrl-a c and configure their environments as needed, and then you can send commands to any screen page (tab) from your script using the "-X stuff" option (to stuff characters into a virtual screen terminal):
$ screen -S demo -p <page_number> -X stuff 'ls -l
'
Note that you also have to send the newline character to really enter the command.
I try add main enviornment path to bash and I success run roscore in another terminal.
#!/bin/bash
# ROS opstarten
PATH=/opt/ros/kinetic/bin
gnome-terminal --tab -e /opt/ros/kinetic/bin/roscore
You need to quote the statements and use a statement separator between them.
gnome-terminal -e 'cd ~/Projects/catkin_ws; source devel/setup.bash; /usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py'
or alternatively with newline as statement separator
gnome-terminal -e 'cd ~/Projects/catkin_ws
source devel/setup.bash
/usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py'
However, running these commands in a separate terminal seems rather misdirected. Why don't you run them as regular background jobs in your current terminal with output to a file?

gnome-terminal new tab with alias as command to execute

I've created an alias in .bashrc file as follows
alias myproject = 'cd ~/Desktop/myproject'
After saving the file when I restart my terminal, typing in myproject takes me to the project directory but when I try to use the alias as a command argument to a new gnome-terminal tab it throws an error,
gnome-terminal --tab -e "myproject"
throws the error
There was an error creating the child process for this terminal
Failed to execute child process "myproject" (No such file or directory)
What is wrong with this ?
When a bash shell is started, per default bash executes the commands specified in .bashrc. This is how your shell knows your aliases.
Now your idea does not work because gnome-terminal never sees your .bashrc file.
You could try
gnome-terminal --working-directory='<path-to-your-home-directory>/Desktop/myproject/
I was trying to do something similar... possibly not exactly what you want, but:
alias startMyRailsProject='cd ~/Desktop/myproject; gnome-terminal --tab --tab -e "rails s" --tab -e "rails c"; exit'
This:
- changes directory to where I want
- starts a new gnome terminal (in the right directory from before)
- creates a 2nd tab and starts my rails server
- creates a 3rd tab and starts my rails console
- and then closes the original terminal window which I call it from.
It does what I need it to and saves a bunch of repetive keystrokes :-)
Cheers
I succeeded in getting some of it to work, I am missing my aliases, but I can run the program I want in the following way:
gnome-terminal --window --title="testtitle" -- $SHELL -c "<path to script/application>/<script/application> <arguments>;"
An example:
gnome-terminal --window --wait --title="testtitle" -- $SHELL -c "echo test;read -p \"press any key to exit\" -n 1 ;"

Attach to 'screen' session with creating a new screen window

I have a screen session running with several windows. I want to attach to it, create a new screen window within it and start a shell in that new window.
Question: How can I do this from the command line outside the screen session?
I already tried a lot, e. g. screen -x (but it attaches to one of the existing screen windows; it does not create a new one nor does it start a new shell). Any hints are welcome.
Environment is Linux (Ubuntu 12.04).
Add new detached window to sesion_name and run command
screen -S sesion_name -x -X screen bash -c 'command; exec bash'
To choose a window to join, use the -p option. Giving + will create a new one, so your command is simply:
screen -x session_name -p +
This is documented on the man page:
-p n̲u̲m̲b̲e̲r̲_o̲r̲_n̲a̲m̲e̲|̲-̲|̲=̲|̲+̲
Preselect a window. This is useful when you want to reattach to a
specific windor or you want to send a command via the "-X" option
to a specific window. As with screen's select commant, "-" selects
the blank window. As a special case for reattach, "=" brings up the
windowlist on the blank window.
I found something on the mailing list, thank you tuxuday :)
I'm doing it now this way:
#!/bin/bash
screen -X screen -t NEWWINDOW # create new window (switches existing attached terminal)
sleep 0.1
screen -X other # switch existing attached terminal back to its old window
sleep 0.1
gnome-terminal -e 'screen -x -p NEWWINDOW' # connect to new window
I'm not sure about those sleeps, maybe they aren't necessary in all environments, but I can wait those 0.2s easily.
My .bash_aliases is changing the screen window title with a delay, so the awkward NEWWINDOW won't stick for long (and thus not hinder further calls to this script).

Resources