How to run command in last focused terminal window using bash? - bash

How can I modify this function to open a new tab in the last focused terminal window, vs opening an entirely new terminal window?
# Opens a new terminal tab and runs the file.
run_in_terminal(){
# $1=file_path
msg="Running $1
in $qube_name Terminal..."
# notify-send "$msg";echo "$msg"
echo "$msg"
sudo gnome-terminal --tab --active --geometry=120X60 \
--title="$1" --working-directory="$code_directory" \
-- bash -c "$1; exec bash"
}

Related

Bash - open a new terminal tab then execute command

The following bash script is suppose to open 2 new terminal tabs then execute respective commands:
mate-terminal --tab -e "cd ~/ece344/root; sys161 -w kernel" --tab -e "cd ~/ece344/root; cs161-gdb kernel"
The script does open 2 new tabs however both tabs display the following error:
There was an error creating the child process for this terminal
Failed to execute child process "cd" (No such file or directory)
Ps. The answer should work with mate-terminal.
I don't have mate installed but I would try :
mate-terminal --tab -e "/bin/bash -c 'cd ~/ece344/root; sys161 -w kernel'" --tab -e "/bin/bash -c 'cd ~/ece344/root; cs161-gdb kernel'"
The idea is that "-e" would want to execute a command that probably run inside the window instead of a default shell, so from the error I understand that "cd" is not a real program in an expected location (since 'cd' is in the PATH shouldn't be a problem.
So my example would provide a full path to a shell "/bin/bash" that would then execute the commands you want.

How to execute multiple commands after opening a new terminal tab

I need to open a new terminal tab and execute multiple commands on it, how can I do it. I have already tried the following,
gnome-terminal --tab -t "X" -e "cd ~/Desktop/terminal_test;mkdir test"
Here I need to cd into a new directory and create a new folder.
Try this:
gnome-terminal -x bash -c "cmd1; cmd2; …cmdN; exec bash"
You can use gnome-terminal. The script below will open 3 tabs running the respective commands..
tab="--tab"
cmd01="bash -c 'ls';'pwd';bash"
foo=""
foo+=($tab -e "$cmd01")
gnome-terminal "${foo[#]}"
exit 0

Guake terminal startup shell script

I'm trying to create a startup script to open two tabs in Guake, execute commands, and then rename the tabs. Unfortunately the tabs are not being renamed. Also irritating, it's opening up the terminal in the directory of the shell script instead of the default ~/ directory. After reading the help file, I'm pretty sure that the script is correct.
Help file:
Options:
-h, --help show this help message and exit
-f, --fullscreen Put Guake in fullscreen mode
-t, --toggle-visibility
Toggles the visibility of the terminal window
-p, --preferences Shows Guake preference window
-a, --about Shows Guake's about info
-n NEW_TAB, --new-tab=NEW_TAB
Add a new tab
-s SELECT_TAB, --select-tab=SELECT_TAB
Select a tab
-g, --selected-tab Return the selectd tab index.
-e COMMAND, --execute-command=COMMAND
Execute an arbitrary command in the selected tab.
-r RENAME_TAB, --rename-tab=RENAME_TAB
Rename the selected tab.
-q, --quit Says to Guake go away =(
And here is my shell script:
#!/bin/sh
# guakeStartup.sh
# open tabs
guake -e "irssi" -r "irssi"
guake -n -e "cd" -r "terminal"
Try it like this inside your startup script:
guake --rename-tab="irssi" --execute-command="irssi" &
sleep 1 &&
guake --new-tab=2 --rename-tab="terminal" --execute-command="cd" &
Or more like your original:
guake -e "irssi" -r "irssi" &
sleep 1 &&
guake -n=2 -e "cd" -r "terminal" &

How to create panes that's won't be destroyed when I quit the command that it's running inside?

I would like to create panes in tmux like so :
$ tmux new-session -d -s mysession "while true; do sleep 1; ls; done"
$ tmux split-window -h "while true; do sleep 1; ls -l; done"
Running it this way, when I cancel the command that is running in the pane (window) I close it immediately. How to avoid this behaviour ?
Use the remain-on-exit window option to mark the window (and any panes it contains) to remain after the command it runs exits.
tmux new-session -d -s mysession "while true; do sleep 1; ls; done"
tmux set-option -t mysession:0 remain-on-exit
When you kill the command, the window will remain, and [the pane] will be labeled "Pane is dead". To restart the same command, issue respawn-window. To start a new command in the window (say, an interactive bash session), issue respawn-window bash.
respawn-window (and respawn-pane, which I forgot about but can be use to target an individual pane within a window) also take a -k option, which can be used to kill whatever is running in a window and either restart the command or start a new command. You could add something to your .tmux.conf like
bind-key C-c respawn-pane -k bash
Then, in any active pane, you can type Control-C to kill whatever is running in the pane and replace it with an interactive shell (remain-on-exit would not be necessary in this case, as you are immediately replacing the old command with a new one).

gnome-terminal cmd leaving processes open after closing parent window

The following simple command should demonstrate my issue:
gnome-terminal --tab -t "Tab 1" -e "/bin/bash" --tab -t "Tab 2" -e "/bin/bash"
This should open a new window with two tabs and an active bash shell. If I immediately close this new window using the GUI [x] and then execute a ps command, one (in this case) sub process (/bin/bash) will be left open. I have found out that this corresponds to all inactive tabs that have never been made active (by clicking on them) at some point in time. So if we crafted a new command similar to the example above but with 5 tabs instead of 2 and you immediately closed this window after running the command, 4 sub-processes will be left running. Here is the peculiar part...if you click on each tab to make it active and then close the window, all of the sub-processes will be killed, as one would expect.
Is this a bug in gnome-terminal?
How can I ensure these processes are closed and not left open?
Run like below:
gnome-terminal --tab -t "Tab 1" -e "bash -ic 'echo Hello; exec bash'" --tab -t "Tab 2" -e "bash -ic 'echo Hello; exec bash'"
You need to run some comand before the ;bash and you can change 'echo Hello' by any other.

Resources