How to open new tabs in gnome-terminal from script - gnome-terminal

I know I can right-click > open tab to open a new tab in gnome-terminal, but how can I do the same from a script? If i use 'gnome-terminal --tab-with-profile=...' it opens a new window.
I need this to be able to open multiple ssh sessions, in tabs, to servers I manage. I don't see any option in ssh to open new sessions in tabs.
I have two different gnome-terminal profiles, a profile 'local' i used for local terminals and a profile 'server' I use for terminals connected to production servers, so it is easy to see which is which. I could open a gnome-terminal, then right-click, open a tab, right-click again and set it to a different profile, then run an ssh command (with switches for non-standard port, key file etc) - but I'd like to condense this into an alias or script for convenience. Hope this clarifies the situation.

try this
//it opens up a window with a tab
gnome-terminal --window --tab

In the version of gnome-terminal in Ubuntu 14 (v3.6?), the command gnome-terminal --tab opens a separate window.
I'm not sure when the behavior changed exactly, but at least as of gnome-terminal v3.28 (Ubuntu 18), gnome-terminal --tab will open a new tab in the current terminal (despite the documentation saying it will open it in the most recently opened window). In this version the additional option --window is needed to open a separate window (ie. gnome-terminal --window --tab).

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

Create Windows 10 shortcut for Cygwin xterm command

In recent years, Cygwins X-windows icon in the notification area doesn't provide an option to start an xterm. If I mistakenly exit my last xterm, I need a way to launch a new one. Currently, I exit X-Windows, remove ~/.server* files, and /tmp/.X*, then restart X-Windows.
I found a command that I can enter after clicking on the Windows Start button:
C:\cygwin64\bin\xterm.exe -display :0
However, it also starts a DOS window in addition to the xterm. If I close the DOS window, the xterm disappears.
I tried the following, with the same effect:
cmd /q /c "C:\cygwin64\bin\xterm.exe -display :0"
cmd /q /c "start /b /min C:\cygwin64\bin\xterm.exe -display :0"
start /b cmd /q /c "C:\cygwin64\bin\xterm.exe -display :0"
start /b C:\cygwin64\bin\xterm.exe -display :0
Note that the two commands that lead with start aren't recognized when I type the command after clicking on the Windows Start button, so I can only use them by manually opening a cmd window. But the problem remains the same; I have a lingering command window.
In separate trials, I also tried putting each of the above three commands into ~/bin/myXterm.bat, clicking the Windows Start button, and typing the full Windows path to myXterm.bat. Same result -- a lingering command window in addition to the desired xterm
Is there a command that won't leave a command window lingering?
I plan to type the command after clicking on the Windows Start button, then right-click the resulting icon in the Taskbar and create an icon from it. I can then find a way to pin it to the start menu or as a persistent shortcut in the Taskbar. It would be preferable if the host shell for xterm did not even appear on the taskbar, as the taskbar is often crowded and extra icons simply create cognitive noise (but small ones for shortcuts are OK).
Note that this question and
this questions are not the same as mine.
What worked
I right-clicked my desktop and chose to create a new shortcut. I specified the following for the Target field
powershell "start C:\cygwin64\bin\xterm.exe -Args \" -display :0 \" -WindowStyle Hidden"
For the Start in field, I specified C:\cygwin64\home\%USERNAME% (use the Cygwin user home directory as it is set up in your installation).
I then right-clicked the shortcut and chose to pin it to the Start Menu.
What doesn't work
I initially put the above command into ~/bin/MyXterm.bat. I can create a shortcut, but I cannot pin it to the Start menu. I don't have that option in the context menu for the BAT file or the shortcut. If I use Windows Explorer to browse to ~/bin, the Applications Tool ribbon has a button to pin the shortcut, but it's grayed out.
I am baffled by why something so simple in the past is so complicated now, costing the afternoon.

I want to run some commands in the same shell script on different tabs/terminal [duplicate]

This question already has answers here:
How to open a new tab in GNOME Terminal from command line? [closed]
(10 answers)
Closed 3 years ago.
I actually want to run some commands of the same script on different tabs or terminal because these commands are activating servers and listening to different ports. So they have to be always active. As well, I want to have a reference to the tab or the terminal so that I can later shut them down at the end of the script. Help please.
I tried a simple script test to see if I could find a way for opening other tabs :
tab=" --tab"
options=()
cmds[1]="echo Banana"
cmds[2]="echo Cat"
for i in 1 2; do
options+=($tab -e "bash -c \"${cmds[i]} ; bash\"" )
done
gnome-terminal "${options[#]}"
exit 0
but I get this as a result :
./test.sh
# 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.
# 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.
Is there any way to open tabs ? and get a reference to each one so that I can shut down commands related to servers once the treatment is done ?
You could use a programm called tmux. It's a terminal-multiplexer, like screen. With that you can open different sessions and windows (these are like tabs) in the same terminal window. They can be referenced by name or id via script.
It's very probable, that the package manager of your Linux distribution has tmux.
P.S: I have to post an answer instead of a comment as I don't have enough reputation for commenting.
This is what I found for man gnome-terminal ; I think that could help , thanks
gnome-terminal(1) General Commands Manual gnome-terminal(1)
NAME
gnome-terminal — is a terminal emulation application.
SYNOPSIS
gnome-terminal [-e, --command=STRING] [-x, --execute ] [--window-
with-profile=PROFILENAME] [--tab-with-profile=PROFILENAME] [--window-
with-profile-internal-id=PROFILEID] [--tab-with-profile-internal-
id=PROFILEID] [--role=ROLE] [--show-menubar] [--hide-menubar]
[--geometry=GEOMETRY] [--working-directory=DIRNAME] [-?, --help]
DESCRIPTION
GNOME Terminal is a terminal emulation application that you can use to
perform the following actions:
Access a UNIX shell in the GNOME environment.
A shell is a program that interprets and executes the commands that you
type at a command line prompt. When you start GNOME Terminal, the
application starts the default shell that is specified in your system
account. You can switch to a different shell at any time.
OPTIONS
-e, --command=STRING
Execute the argument to this option inside the terminal.
-x, --execute
Execute the remainder of the command line inside the termi‐
nal.
--window-with-profile=PROFILENAME
Open a new window containing a tab with the given profile.
More than one of these options can be provided.
--tab-with-profile=PROFILENAME
Open a tab in the window with the given profile. More than
one of these options can be provided, to open several tabs .
--window-with-profile-internal-id=PROFILEID
Open a new window containing a tab with the given profile ID.
Used internally to save sessions.

Linux: Gedit won't run in foreground

If I run gedit in the Linux terminal ($ gedit) it opens as a background job; which is not what I'm after at the moment.
Other programs (such as emacs) run in the foreground with commands like ($ emacs) and only run in the background if I've specified it via something like ($ emacs &).
I've tried searching for a solution but almost everything is about the opposite (trying/struggling to get things to run in the background).
Any ideas?
As it works for me: I run gedit /tmp/file.txt for the first time and it runs in the foreground. Then I open another terminal tab and run gedit /tmp/file2.txt while Gedit is still open — the second command instructs running instance of Gedit to open second tab and exits immediately. Gedit is still in the foreground in the first terminal tab.
According to gedit help, it has an option
-w, --wait Open files and block process until files are closed
If in the second terminal tab I run gedit --wait /tmp/file3.txt, then it opens a new tab in the existing Gedit window but the command stays in the foreground until I close that file tab.
Just in case: it was tested under KDE, Ubuntu 16.04, gedit version is 3.18.3

How do I open a new window (shell) from command line in Linux?

I'm working with a tool right now that requires me to putty to a remote host, login, run a series of commands to start an engine, open a new window (and login again) to start a different engine, then open a third window (and again, login) to actually use the tool (leaving the engines running in those first two windows). I'd like to write a shell script to automate the process so that I could just open one window, type "sh whatever.sh" and be off and running, without physically opening the new windows and logging in again. However, I can't find a command to get me from one window to the next. Any thoughts?
You can just background the first processes by adding an ampersand (&) to the command line or pressing Ctrl+Z when it is running (and then enter bg to let the process continue, more information about that with jobs).
If that's not enough, you can create virtual shells with screen or tmux.
If you've redirected X (i.e. you can access GUIs over ssh), you can also just start a new window by executing your favorite (GUI) console program, like xterm, konsole, gnome-terminal, etc.
Are you familiar with jobs on linux?
nohup whatever_1.sh &
nohup whatever_2.sh &
nohup whatever_3.sh &
Or perhaps screen would be of use here:
https://serverfault.com/questions/25301/job-control-and-ssh
See also, nohup:
http://en.wikipedia.org/wiki/Nohup
The bash command opens a Bourne-again shell (bash) session.
Try typing in "konsole". That should open a new bash window and set the focus to it.
On my Ubuntu 18 I just type the command:
gnome-terminal
and a new shell opens... I don't like the above answers because xterm and konsole most likely not already be installed.
Shell script on target machine cannot be aware of putty windows on client machine.
Consider using Screen : http://www.gnu.org/s/screen/ - it is clean and powerful way.
I think you need command line window then write:
$ xterm
# new window started
If you need python in new window:
$xterm python
#now a window will shown with python shell
Another nice option from Xfce's terminal:
xfce4-terminal

Resources